Home > AI > Language > Python >

translate WeChat Chinese to English in bulk

# for python-docx package
# create words documents
from docx import Document
from docx.shared import Inches

from deep_translator import GoogleTranslator
from tqdm import tqdm

def getChinese():
    document = Document('Statement.docx')
    # first table, 2nd row, 1st column
    return document.text


def translate(txt):
    document = Document('contact-transcript.docx')
    table = document.tables[0]

    ps = txt.split('\n')
    tmp_cn = ""
    tmp_en = ""
    for p in tqdm(ps):
        if p == "":
            continue

        elif "—————" in p:
            row = table.add_row().cells
            row[0].text = p
            continue

        elif "R" in p or "moon" in p:
            tmp_cn = p + "\n"
            tmp_en = p + "\n"
            continue

        try:
            result = GoogleTranslator(source='auto', target='en').translate(p)
            print(p, result)

            tmp_cn += p
            tmp_en += result

            row = table.add_row().cells
            row[0].text = tmp_cn
            row[1].text = tmp_en

            tmp_cn = ""
            tmp_en = ""

        except:
            print(p, "error")

            tmp_cn += p
            row = table.add_row().cells
            row[0].text = tmp_cn

            tmp_cn = ""

    document.save('good.docx')




if __name__ == '__main__':
    txt = getChinese()
    print(txt)




Leave a Reply