-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtradutor_pdf.py
46 lines (37 loc) · 1.43 KB
/
tradutor_pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import asyncio
import json
from googletrans import Translator
from gtts import gTTS
import os
from PyPDF2 import PdfReader
async def read_pdf(file_path):
try:
with open(file_path, 'rb') as f:
pdf = PdfReader(f)
num_pages = len(pdf.pages)
text = ''
for page in range(num_pages):
page_obj = pdf.pages[page]
text += page_obj.extract_text()
translator = Translator()
translation = await translator.translate(text, dest='pt')
translated_text = translation.text
# Crie um arquivo de áudio com a tradução
tts = gTTS(text=translated_text, lang='pt')
tts.save('tradução.mp3')
# Reproduza o arquivo de áudio
os.system('start tradução.mp3')
except FileNotFoundError:
print(f"File not found: {file_path}")
print("Make sure the file is in the correct location and the path is accurate.")
except (json.JSONDecodeError, TypeError):
print("Error decoding JSON response or unexpected response format.")
print("This could be due to:")
print(" - A temporary issue with the Google Translate API.")
print(" - API rate limits.")
print(" - An invalid input.")
print("Please try again later or check your input.")
async def main():
file_path = 'estatistica.pdf'
await read_pdf(file_path)
asyncio.run(main())