以下是实现以上功能的Python代码:

from tkinter import *
from tkinter import filedialog
import docx

def open_file():
    global eng_file_path
    eng_file_path = filedialog.askopenfilename(filetypes=[("Word Files", "*.docx")])
    eng_file_label.config(text=eng_file_path)

def open_translation_file():
    global chi_file_path
    chi_file_path = filedialog.askopenfilename(filetypes=[("Word Files", "*.docx")])
    chi_file_label.config(text=chi_file_path)

def generate_translation():
    if not eng_file_path or not chi_file_path:
        messagebox.showerror("Error", "Please select both English and Chinese translation files.")
        return

    eng_doc = docx.Document(eng_file_path)
    chi_doc = docx.Document(chi_file_path)

    eng_paras = [p.text.strip() for p in eng_doc.paragraphs if p.text.strip()]
    chi_paras = [p.text.strip() for p in chi_doc.paragraphs if p.text.strip()]

    if len(eng_paras) != len(chi_paras):
        messagebox.showwarning("Warning", "The number of paragraphs in English and Chinese translation files do not match.")

    output_doc = docx.Document()
    for i in range(max(len(eng_paras), len(chi_paras))):
        eng_para = eng_paras[i] if i < len(eng_paras) else ""
        chi_para = chi_paras[i] if i < len(chi_paras) else ""

        output_doc.add_paragraph(eng_para, style="List Bullet")
        output_doc.add_paragraph(chi_para, style="List Bullet")

        if i == len(eng_paras):
            output_doc.add_paragraph("No English translation available for this paragraph.", style="List Bullet")
        elif i == len(chi_paras):
            output_doc.add_paragraph("No Chinese translation available for this paragraph.", style="List Bullet")

    output_path = filedialog.asksaveasfilename(defaultextension=".docx")
    if output_path:
        output_doc.save(output_path)

root = Tk()
root.title("Bilingual Translator")

eng_file_path = ""
chi_file_path = ""

eng_file_button = Button(root, text="Select English File", command=open_file)
eng_file_button.pack(pady=10)

eng_file_label = Label(root, text="")
eng_file_label.pack()

chi_file_button = Button(root, text="Select Chinese Translation File", command=open_translation_file)
chi_file_button.pack(pady=10)

chi_file_label = Label(root, text="")
chi_file_label.pack()

generate_button = Button(root, text="Generate Translation", command=generate_translation)
generate_button.pack(pady=10)

root.mainloop()

代码中使用了Python的Tkinter库来创建GUI界面,使用了python-docx库来读取和写入Word文档。在生成双语译文时,首先读取英文和中文译文文件中的段落内容并去除空行,然后根据两个文件中段落的数量来生成双语译文文件。如果两个文件中段落的数量不匹配,则在输出文件中添加相应的提示信息。最后,使用文件对话框来让用户选择输出文件的路径和文件名

用python实现以下功能:一个英文word文件一个对应的中文word译文文件用户选择英文word文件和中文word译文文件点击生成按钮生成一段英文一段中文的按段落进行对照的双语译文文件要求生成译文的时候去掉原文word和译文word中空行并在生成双语译文的时候考虑到原文和译文段落数不匹配的情况如果原文和译文段落数不匹配也需要生成双语译文文件但没有匹配的内容需要单独分隔并说明而且在任务完成后需要向

原文地址: http://www.cveoy.top/t/topic/ens3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录