PowerShell/Docs/speak-epub.md

143 lines
3.7 KiB
Markdown
Raw Normal View History

2023-07-29 10:34:04 +02:00
*speak-epub.ps1*
================
2021-11-08 21:36:42 +01:00
2022-02-10 09:01:07 +01:00
This PowerShell script speaks the content of the given Epub file by text-to-speech (TTS).
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2023-07-29 10:15:44 +02:00
PS> ./speak-epub.ps1 [[-Filename] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
-Filename <String>
Specifies the path to the Epub file
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
PS> ./speak-epub C:\MyBook.epub
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2023-05-26 12:20:18 +02:00
Speaks an Epub file by text-to-speech (TTS).
2022-11-17 20:02:26 +01:00
.DESCRIPTION
This PowerShell script speaks the content of the given Epub file by text-to-speech (TTS).
.PARAMETER Filename
Specifies the path to the Epub file
.EXAMPLE
PS> ./speak-epub C:\MyBook.epub
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$Filename = "")
function Speak { param([string]$Text)
write-output "$Text"
$Voice = new-object -ComObject SAPI.SPVoice
$Voices = $Voice.GetVoices()
foreach ($OtherVoice in $Voices) {
$Description = $OtherVoice.GetDescription()
if ($Description -like "*- English (United States)") {
$Voice.Voice = $OtherVoice
break
}
}
[void]$Voice.Speak($Text)
}
function ReadBook() { param([string]$book, [string]$bookPath, [int]$lineNumber = 0)
$data = Get-Content $book
for([int]$i=$lineNumber;$i -lt $data.Count;$i++) {
Set-Content -Path $bookPath"\progress.txt" -Value ($book+","+$i)
$line = $data[$i]
if ($line.Contains("<title>")) {
$line = $line -Replace "<.+?>",""
Speak $line
}
if ($line.contains("<p")) {
$line = $line -Replace "<.+?>",""
Speak $line
}
}
Set-Content -Path $bookPath"\progress.txt" -Value ("")
}
function UnzipFile() { param([string]$file, [string]$dest)
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file)
foreach($item in $zip.items()) {
$shell.Namespace($dest).copyhere($item)
}
}
if ($Filename -eq "") {
2023-05-26 12:20:18 +02:00
$Filename = Read-Host "Enter path to .epub file"
2022-11-17 20:02:26 +01:00
}
write-output "Reading $Filename ..."
$file = get-item $Filename
if (-not(Test-Path $file.DirectoryName+"\"+$file.Name+".zip")) {
$zipFile = $file.DirectoryName+"\"+$file.Name+".zip"
$file.CopyTo($zipFile)
}
$destination = $file.DirectoryName+"\"+$file.Name.Replace($file.Extension,"")
if (-not(Test-Path $destination)) {
md $destination
UnzipFile -file $zipFile -dest $destination
}
[xml]$container = Get-Content $destination"\META-INF\container.xml"
$contentFilePath = $container.container.rootfiles.rootfile."full-path"
[xml]$content = Get-Content $destination"\"$contentFilePath
$tmpPath = Get-Item $destination"\"$contentFilePath
$bookPath = $tmpPath.DirectoryName
$progress = $null
foreach($item in $content.package.manifest.Item) {
if ($item."media-type" -eq "application/xhtml+xml") {
if (Test-Path $bookPath+"\progress.txt") {
$progress = Get-Content $bookPath"\progress.txt"
$progress = $progress.Split(",")
}
$bookFileName = $item.href
if ($progress.Count -eq 2) {
if ($progress[0] -eq $bookPath+"\"+$bookFileName) {
ReadBook -book $bookPath"\"$bookFileName -bookPath $bookPath -lineNumber $progress[1]
}
}
else {
ReadBook -book $bookPath"\"$bookFileName -bookPath $bookPath
}
}
}
2023-05-26 12:20:18 +02:00
exit 0 # success
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2023-07-29 10:34:04 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of speak-epub.ps1 as of 07/29/2023 10:33:49)*