from gtts import gTTS
import pdfplumber
from pathlib import Path
def pdf_to_mp3(file_path='test.pdf', language='en'):
if Path(file_path).is_file() and Path(file_path).suffix == '.pdf':
#return 'Файл прошел проверку, все good!'
with pdfplumber.PDF(open(file=file_path,mode='rb')) as pdf:
pages = [page.extract_text() for page in pdf.pages]
text = ''.join(pages)
text = text.replace('\n', '')
with open('text1.txt', 'w', encoding='utf-8') as file:
file.write(text)
else:
return 'Файл поврежден или не подходит!'
def main():
print(pdf_to_mp3(file_path=r"C:\Users\hello\Desktop\Voice Enhancer\enc.pdf"))
if __name__ == '__main__':
main()
open('text1.txt', 'w', encoding='utf-8')
srcpath = Path(file_path)
if srcpath.is_file() and srcpath.suffix == '.pdf':
...
with (srcpath.parent / 'text1.txt').open('wt', encoding='utf-8') as file:
...