簡體   English   中英

Pyttsx3 長輸入文件導致只有最后一頁被轉換為 MP3

[英]Pyttsx3 long input file results in only the last page being converted to MP3

import pyttsx3,PyPDF2
import os

file_path = input('Enter a file path:')

if os.path.exists(file_path):   
    pdfreader = PyPDF2.PdfFileReader(open(file_path, 'rb'))
    engine = pyttsx3.init()

    for page_num in range(pdfreader.numPages):
        text = pdfreader.getPage(page_num).extract_text()
        clean_text = text.strip().replace('\n', '    ')
        print(clean_text)

    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    rate = engine.getProperty('rate')
    engine.setProperty('rate', rate-55)
    engine.save_to_file(clean_text, 'story.mp3')
    engine.runAndWait()

    engine.stop()

    
else:
    print('The specified file does not exist')   

當加載我大約 70 頁的論文工作時,它會完全打印出 clean_text,但在 mp3 中只讀出最后一頁。 我錯過了什么嗎?

自己找的。 必須將 for 循環中的字符串連接起來,以便將所有內容一起讀出。

在它外面定義了一個新的字符串

final_text=""

並將其添加到 for 循環中:

final_text += clean_text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM