Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16803

Errors with Python program to translate word doc from English to French

$
0
0

I am very new to programming and am trying to use python 3.12.3 on Windows 11 to take a word doc file and translate it from English to French for a student of mine. It is a very large file (84,332kb) that I coverted from a PDF.

from docx import Documentfrom googletrans import Translatordef translate_docx(input_file, output_file):    # Open the input Word document    doc = Document(input_file)    # Create a translator object    translator = Translator()    # Initialize an empty Document object for the translated document    translated_doc = Document()    # Translate each paragraph in the input document    for para in doc.paragraphs:        # Translate the text from English to French        translated_text = translator.translate(para.text, src='en', dest='fr').text        # Add the translated text to the translated document        translated_doc.add_paragraph(translated_text)    # Save the translated document to the specified file    translated_doc.save(output_file)translate_docx(r"C:\Users\shane\coding\amtg_handbook.docx", r"C:\Users\shane\coding\amtg_handbook_french.docx")

I am getting this error.

Traceback (most recent call last):File "C:\Users\shane\coding\new.py", line 26, in translate_docx(r"C:\Users\shane\coding\amtg_handbook.docx", r"C:\Users\shane\coding\amtg_handbook_french.docx")File "C:\Users\shane\coding\new.py", line 17, in translate_docxtranslated_text = translator.translate(para.text, src='en', dest='fr').text^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\shane\AppData\Local\Programs\Python\Python312\Lib\site-packages\googletrans\client.py", line 219, in translateparsed = json.loads(data[0][2])^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\shane\AppData\Local\Programs\Python\Python312\Lib\json_init_.py", line 339, in loadsraise TypeError(f'the JSON object must be str, bytes or bytearray, 'TypeError: the JSON object must be str, bytes or bytearray, not NoneType

Any help would be appreciated as this is a bit above my skill level with python.

I have also tried this using the original PDF.

import fitzfrom googletrans import Translatordef translate_pdf(input_file, output_file):    # Open the input PDF file    input_pdf = fitz.open(input_file)    # Create a translator object    translator = Translator()    # Initialize an empty PDF writer    output_pdf = fitz.open()    # Iterate through each page of the input PDF    for page_num in range(input_pdf.page_count):        # Get the text of the current page        page = input_pdf.load_page(page_num)        text = page.get_text()        # Translate the text from English to French        translated_text = translator.translate(text, src='en', dest='fr').text        # Create a new page in the output PDF with the translated text        output_page = output_pdf.new_page(width=page.rect.width, height=page.rect.height)        output_page.insert_text((10, 10), translated_text)    # Save the output PDF to the specified file    output_pdf.save(output_file)    # Close the input and output PDF files    input_pdf.close()    output_pdf.close()translate_pdf(r"C:\Users\shane\coding\amtg_handbook.pdf", r"C:\Users\shane\coding\amtg_handbook_french.pdf")

and it resulted in this error.

Traceback (most recent call last):File "C:\Users\shane\coding\PDF_English_French.py", line 35, in translate_pdf(r"C:\Users\shane\coding\amtg_handbook.pdf", r"C:\Users\shane\coding\amtg_handbook_french.pdf")File "C:\Users\shane\coding\PDF_English_French.py", line 21, in translate_pdftranslated_text = translator.translate(text, src='en', dest='fr').text^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\shane\AppData\Local\Programs\Python\Python312\Lib\site-packages\googletrans\client.py", line 222, in translatetranslated_parts = list(map(lambda part: TranslatedPart(part[0], part[1] if len(part) >= 2 else []), parsed[1][0][0][5]))^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\shane\AppData\Local\Programs\Python\Python312\Lib\site-packages\googletrans\client.py", line 222, in translated_parts = list(map(lambda part: TranslatedPart(part[0], part[1] if len(part) >= 2 else []), parsed[1][0][0][5]))~~~~^^^IndexError: list index out of range


Viewing all articles
Browse latest Browse all 16803

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>