Table of Contents
1. Requirements description" >1. Requirements description
2. Logical sorting" >2. Logical sorting
##3. Overall implementation steps" >##3. Overall implementation steps
三、代码实现" >三、代码实现
Home Backend Development Python Tutorial Document batch translation tool written in Python, the effect is better than paid software?

Document batch translation tool written in Python, the effect is better than paid software?

Aug 09, 2023 pm 05:37 PM
python translate


##This article will share with you a practical Python office automation script

"Use Python to batch translate English Word documents and preserve the format", the final effect is even better than some paid software! Let’s take a look at the specific work content first.

1. Requirements description

I have a large number of foreign language documents on hand (this case takes

5 as an example, and they are named test1 .docx test2.docx and so on), one of which is as follows: Document batch translation tool written in Python, the effect is better than paid software?

Basic requirements:"Batch these documents All the contents are translated into Chinese and transferred to a new file", the effect is as follows: Document batch translation tool written in Python, the effect is better than paid software?

Advanced requirements: While the basic needs are met, the requirements『Keep the format of the original document』, the effect is as follows:Document batch translation tool written in Python, the effect is better than paid software?

2. Logical sorting

1 . Translation API

The core of this requirement is

Translation. The strategy is to use the translation API of the network. The Baidu Translation Open Platform is recommended here. It can be used if the number of concurrency is not considered. Standard version, free to use with no character limit!

Baidu Translation Open Platform: http://api.fanyi.baidu.com/api/trans/product/index

Before using Baidu’s universal translation API, you need to complete the following tasks:

  1. Use a Baidu account to log in to the Baidu Translation Open Platform (http://api.fanyi.baidu.com);
  2. Register as a developer and obtain APPID;
  3. Conduct developer certification (if you only need the standard version, you can skip it);
  4. Open the universal translation API service: activation link
  5. Refer to the technical documentation Write code with Demo
Document batch translation tool written in Python, the effect is better than paid software?

After completion, you can see the ID and key on the personal page. This is very important! The demo of the compiled universal translation API is given below. The output has been simply modified, and the code can be used! Document batch translation tool written in Python, the effect is better than paid software?Document batch translation tool written in Python, the effect is better than paid software?

You can see that the test content is accurately translated. Note that if you need to access the API multiple times, the free version has concurrency and time limits, you can use time The module sleeps for one second

2. Format modification

The difficulty with advanced requirements is to retain the format. To put it simplyoriginal What is the page format and paragraph format of the document, and what are the corresponding parts after translation.

Based on the above logical relationship, you only need to obtain the corresponding content of the original document and assign it to the newly translated document. (For the time being, it can only meet the unification of page settings and paragraph settings. For the format modification of specific words in a paragraph, ensuring accuracy requires natural language processing NLP, which is not covered in this article)

2.1 Page style

The page style only needs to include margins, direction, height, width, etc., as can be seen from the original document, the following is Narrow margins. But we don’t need to know how to set the four directions of narrow margins. We only need to present the variable transfer of the old and new documents in the code, as followsDocument batch translation tool written in Python, the effect is better than paid software?

2.2 Paragraph style

Paragraph styles include alignment, indentation, spacing, etc. In the original document, post-paragraph indentation is adopted, and the title is centered. These settings can be done well in variable passing. If the variable value not set in the original document is NoneDocument batch translation tool written in Python, the effect is better than paid software?

2.3 Text block style modification

for To adjust styles such as font size, bold, italics, and color, the strategy adopted is to create an empty list, traverse each text block of each paragraph of the original document, obtain the corresponding attributes and put them in their respective lists , and for the same paragraph For example, the option that contains the most text block attributes is assigned to the corresponding paragraph of the translated document (if all or most of the text in a paragraph is bold, then all text blocks in the corresponding paragraph after translation will be set to bold) Readers who are interested in NLP can try on their own how to highly restore the style modifications of certain specific words in English documents and reflect them in the translated documentsDocument batch translation tool written in Python, the effect is better than paid software?

The above code does not include font settings , because there is no need to pass English fonts to Chinese documents. The setting of Chinese fonts has been mentioned in previous articles. It is relatively complicated. See the code directly:

from docx.oxml.ns import qn

run.font.name = '微软雅黑'
r = run._element.rPr.rFonts
r.set(qn('w:eastAsia'), '微软雅黑')

##3. Overall implementation steps

Now each part of the operation has been completed. Considering that there are multiple documents that need to be translated in this example, the entire logic is as follows:

  1. 利用 glob 模块批处理框架可获取某个文件的绝对路径
  2. python-docx 完成 Word 文件实例化后对段落进行解析
  3. 解析出的段落文本交给百度通用翻译 API,解析返回的 Json 格式结果(上面的修改 demo 中已经完成了这一步)并重新写入新的文件
  4. 同个文件全部解析、翻译并写入新文件后保存文件

三、代码实现

导入需要的模块,除翻译 demo 中需要的库外还需要 glob 库批量获取文件、python-docx 读取文件、time 模块控制访问并发。为什么要 os 模块见下文:

import requests
import random
import json
from hashlib import md5
import time
from docx import Document
import glob
import os

对原 demo 的部分内容进行保留,涉及到 query 参数的代码需要移动到后面的循环中。保留的部分:Document batch translation tool written in Python, the effect is better than paid software?

效果如下Document batch translation tool written in Python, the effect is better than paid software?

获取到段落文本后,可以将段落文本赋值给 query 参数,调用 API demo 的后续代码。输出结果的同时用 add_paragraph 将结果写入新文档:Document batch translation tool written in Python, the effect is better than paid software?

最后保存成新文件,期望命名为 原文件名_translated 的形式,可用 os.path.basename 方法获取并经字符串拼接达到目的:

wordfile_new.save(path + r'\\' + os.path.basename(file)[:-5] + '_translated.docx')
Document batch translation tool written in Python, the effect is better than paid software?

单个文件操作完成后将读取和创建文件的代码块放到批处理框架内:Document batch translation tool written in Python, the effect is better than paid software?

完成了上面的内容后,基本需求就完成了。根据我们梳理的对样式的修改知识,再把样式调整的代码加进来就行了,最终完整代码如下:Document batch translation tool written in Python, the effect is better than paid software?

代码运行完毕后得到五个新的翻译后文件Document batch translation tool written in Python, the effect is better than paid software?

翻译效果如下,可以看到英文被翻译成中文,并且样式大部分保留!Document batch translation tool written in Python, the effect is better than paid software?

至此,所有文档都被成功翻译,当然这是机器翻译的,具体应用时还需要对关键部分进一步人工调整,不过整体来说还是一次成功的Python办公自动化尝试!

The above is the detailed content of Document batch translation tool written in Python, the effect is better than paid software?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install packages from a requirements.txt file in Python How to install packages from a requirements.txt file in Python Sep 18, 2025 am 04:24 AM

Run pipinstall-rrequirements.txt to install the dependency package. It is recommended to create and activate the virtual environment first to avoid conflicts, ensure that the file path is correct and that the pip has been updated, and use options such as --no-deps or --user to adjust the installation behavior if necessary.

How to handle command line arguments in Python How to handle command line arguments in Python Sep 21, 2025 am 03:49 AM

Theargparsemoduleistherecommendedwaytohandlecommand-lineargumentsinPython,providingrobustparsing,typevalidation,helpmessages,anderrorhandling;usesys.argvforsimplecasesrequiringminimalsetup.

How to test Python code with pytest How to test Python code with pytest Sep 20, 2025 am 12:35 AM

Python is a simple and powerful testing tool in Python. After installation, test files are automatically discovered according to naming rules. Write a function starting with test_ for assertion testing, use @pytest.fixture to create reusable test data, verify exceptions through pytest.raises, supports running specified tests and multiple command line options, and improves testing efficiency.

From beginners to experts: 10 must-have free public dataset websites From beginners to experts: 10 must-have free public dataset websites Sep 15, 2025 pm 03:51 PM

For beginners in data science, the core of the leap from "inexperience" to "industry expert" is continuous practice. The basis of practice is the rich and diverse data sets. Fortunately, there are a large number of websites on the Internet that offer free public data sets, which are valuable resources to improve skills and hone your skills.

What is BIP? Why are they so important to the future of Bitcoin? What is BIP? Why are they so important to the future of Bitcoin? Sep 24, 2025 pm 01:51 PM

Table of Contents What is Bitcoin Improvement Proposal (BIP)? Why is BIP so important? How does the historical BIP process work for Bitcoin Improvement Proposal (BIP)? What is a BIP type signal and how does a miner send it? Taproot and Cons of Quick Trial of BIP Conclusion‍Any improvements to Bitcoin have been made since 2011 through a system called Bitcoin Improvement Proposal or “BIP.” Bitcoin Improvement Proposal (BIP) provides guidelines for how Bitcoin can develop in general, there are three possible types of BIP, two of which are related to the technological changes in Bitcoin each BIP starts with informal discussions among Bitcoin developers who can gather anywhere, including Twi

How to choose a computer that is suitable for big data analysis? Configuration Guide for High Performance Computing How to choose a computer that is suitable for big data analysis? Configuration Guide for High Performance Computing Sep 15, 2025 pm 01:54 PM

Big data analysis needs to focus on multi-core CPU, large-capacity memory and tiered storage. Multi-core processors such as AMDEPYC or RyzenThreadripper are preferred, taking into account the number of cores and single-core performance; memory is recommended to start with 64GB, and ECC memory is preferred to ensure data integrity; storage uses NVMeSSD (system and hot data), SATASSD (common data) and HDD (cold data) to improve overall processing efficiency.

How can you create a context manager using the @contextmanager decorator in Python? How can you create a context manager using the @contextmanager decorator in Python? Sep 20, 2025 am 04:50 AM

Import@contextmanagerfromcontextlibanddefineageneratorfunctionthatyieldsexactlyonce,wherecodebeforeyieldactsasenterandcodeafteryield(preferablyinfinally)actsas__exit__.2.Usethefunctioninawithstatement,wheretheyieldedvalueisaccessibleviaas,andthesetup

How to write automation scripts for daily tasks in Python How to write automation scripts for daily tasks in Python Sep 21, 2025 am 04:45 AM

Identifyrepetitivetasksworthautomating,suchasorganizingfilesorsendingemails,focusingonthosethatoccurfrequentlyandtakesignificanttime.2.UseappropriatePythonlibrarieslikeos,shutil,glob,smtplib,requests,BeautifulSoup,andseleniumforfileoperations,email,w

See all articles