SoFunction
Updated on 2024-11-15

Python automated word modification case

preamble

Using Python docx module, you can easily open and modify Word 2007 and later documents. This article briefly describes how to use python to modify the contents of word documents.

Examples and Code

The content of the word document is a letter of commendation, the content of which is shown below:

Letter of commendation.png

Now we need to modify the "commendation letter.docx" document by python, the required changes are marked in the figure.

1. At the first arrow, indent the first line by 2 characters

2. At the second arrow, left-indent the paragraph by 2 characters and add "Learn from Mr. Z!"

3, the third and fourth arrow, for right alignment, and right indentation 2cm

4. Zhao Donglai, modified to Xiaoz

5. Luk may also be amended to read Big Z

6. She, modified to he

7. Dog food, modified to cat food

The code is as follows:

from docx import Document
from  import Cm
from  import WD_ALIGN_PARAGRAPH
import re 
document=Document(r"g:\CS\Python Scripts\ commendation letter.docx")
# First of all, modify the paragraph formatting, docx default title also belongs to the paragraph, so "letter of commendation" is the first paragraph
paragraphs=
paragraphs[2].paragraph_format.first_line_indent=Cm(0.74)
paragraphs[3].paragraph_format.left_indent=Cm(0.74)
paragraphs[4].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.RIGHT
paragraphs[4].paragraph_format.right_indent=Cm(2)
paragraphs[5].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.RIGHT
paragraphs[5].paragraph_format.right_indent=Cm(2)
# Make changes to the text
# Revise the second paragraph
paragraphs[1].text="Mr. Little Z:"
# Replace Lu with Big Z and her with him in the third paragraph. With python's regular expressions, it's easy to replace and find text.
text=("Lu may also,'Big Z',paragraphs[2].text)
text=('She','He',text)
paragraphs[2].text=text
# Add after the fourth paragraph
paragraphs[3].add_run("Learn from Mr. Z!")
# Modify the contents of the table
tables=
tables[0].cell(1,0).text="Cat food."
tables[0].cell(2,0).text="Cat food."
tables[0].cell(3,0).text="Cat food."
# Insert an image with the width set to 11.8cm
document.add_picture('', width=Cm(11.8))
()

Run the code, the result is shown in the figure below:

Modified.png

In this paper, the content in the word document is modified mainly in the following aspects:

Replacement and addition of paragraph text

Alignment and indentation of paragraph alignment

Changes to table text

Inserting Pictures in Documents

The docx module is powerful and also supports the modification of word documents, fonts, colors, styles, chapters, page breaks, tabs and so on. Through python batch operation of word documents, you can effectively save time, say goodbye to tedious and repetitive work.

reach a verdict

By manipulating word document paragraphs, tables and images, combined with python's regular expressions, at the moment, it is possible to think of functions that can be realized as follows:

1, batch production word business cards, letters, notices, etc. (features similar to word mail merge function)

2, the content of multiple documents for one-key extraction and replacement

3, through the docx module, python will be Excel, e-mail, web pages, matplotlib, support python interface software and other resources together, to extract the relevant information, automatically generate word reports

4. Playing hard to get in front of your girlfriend

Subsequent time, will be on the docx module paragraph (paragraph), table (table) and other objects in detail.

Supplementary: Python to modify the content of Word documents and insert images

The code is as follows

#!/usr/bin/python
# coding:utf-8
import docx
def main():
  filepath = ''
  filepath1 = ''
  newdocx = (filepath)
  table = 
  for oTable in table:
    rows_num = len()
    columns_num = len()
    cell = (3, 0)
    # cell.add_paragraph("a")
     = ""
    [-1].runs[0].add_picture('')
    print(rows_num)
    print(columns_num)
    (filepath1)
if __name__ == '__main__':
  main()

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more. If there is any mistake or something that has not been fully considered, please do not hesitate to give me advice.