Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common methods for Python to operate Word document docx

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what are the common methods for Python to operate Word document docx. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

Installation

Docx is a non-standard library, which can be installed by using pip on the command line (terminal).

Pip install python-docx

It is important to note that the installation is python-docx and the actual call is docx!

Pre-knowledge

Generally speaking, Word can be structured into three parts:

Document Document

Paragraph Paragraph

Text block Run

That is, the Document-Paragraph-Run three-tier structure, which is the most common case. The Chinese character block Run is the most difficult to understand and cannot be completed. according to the figure, the short sentence between the two symbols is a text block.

Normally, it can be understood this way, but if there are many different styles in this short sentence, it will be divided into multiple text blocks. take the first yellow circle in the picture as an example, if you add some details to the short sentence.

At this point, there are four blocks of text, and sometimes there are tables in an Word document, and a new document structure is generated.

At this time, the structure is very similar to Excel, which can be regarded as Document-Table-Row/Column-Cell four-level structure.

Word read

1. Open Word

From docx import Document path =... wordfile = Document (path)

two。 Get paragraph

A word file consists of one or more paragraph paragraphs

Paragraphs = wordfile.paragraphs print (paragraphs)

3. Get the text content of a paragraph

Get text with .text

For paragraph in wordfile.paragraphs: print (paragraph.text)

4. Get text block text content

A paragraph paragraph consists of one or more run text blocks

For paragraph in wordfile.paragraphs: for run in paragraph.runs: print (run.text)

5. Traversing the table

The above operation completes the traversal of the classic three-level structure, traversing the table is very similar.

# traversing for table in wordfile.tables: for row in table.rows: for cell in row.cells: print (cell.text) # traversing for table in wordfile.tables: for column in table.columns: for cell in column.cells: print (cell.text) by column

Write to Word

1. Create Word

As long as no path is specified, a new Word file is created by default

From docx import Document wordfile = Document ()

two。 Save Fil

Remember to save the changes and creation of the document

Wordfile.save (...) Put the path that needs to be saved

3. Add title

Wordfile.add_heading (… , level=...)

4. Add paragraph

Wordfile.add_paragraph (...)

Wordfile = Document () wordfile.add_heading ('first-level heading', level=1) wordfile.add_paragraph ('new paragraph')

5. Add text block

Wordfile.add_run (...)

6. Add paging

Wordfile.add_page_break (...)

7. Add Picture

Wordfile.add_picture (..., width=... , height=...)

Set styl

1. Text font settin

two。 Text other style settin

From docx import Document from docx.shared import RGBColor Pt wordfile = Document (file) for paragraph in wordfile.paragraphs: for run in paragraph.runs: run.font.bold = True # bold run.font.italic = True # italic run.font.underline = True # underline run.font.strike = True # delete line run.font.shadow = True # Shadow run.font.size = Pt (20) # font size run.font.color.rgb = RGBColor 0,0) # font color

3. Paragraph style settin

The default alignment is left alignment, which can be modified by yourself.

These are all the contents of this article entitled "what are the common ways for Python to operate Word document docx?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report