简体   繁体   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')   

When loading in my thesis work which is about 70 pages, it prints out the clean_text comletely, but in the mp3 only the last page is being read out.当加载我大约 70 页的论文工作时,它会完全打印出 clean_text,但在 mp3 中只读出最后一页。 Am I missing something?我错过了什么吗?

Found it myself.自己找的。 Had to concatenate the strings from the for loop in order for everything to be read out together.必须将 for 循环中的字符串连接起来,以便将所有内容一起读出。

Defined a new string outside it在它外面定义了一个新的字符串

final_text=""

and added it to the for loop:并将其添加到 for 循环中:

final_text += clean_text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM