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

How to read and write word documents with Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to use Python to read and write word documents", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to read and write word documents with Python.

1.Python writes word documents

To manipulate word documents, you first need to install the python-docx library.

Pip install python-docx

Then import the docx module, or import the Document class from the docx module

From docx import Document

Then use Document () to create an word document, or open the document if you specify the path

Document = Document ()

You can then insert content into the document. For example, you can use the add_heading () method to insert a title, where the parameter level is the title level, 0 represents the first-level title, 1 represents the second-level title, and so on.

You can use the add_paragraph () method to insert a paragraph. The parameter style is a style, and no style is applied by default.

There are other things, such as the add_picture () method for inserting pictures, the add_table () method for inserting tables, and so on.

Finally, as with Excel, after adding content to the document, you need to use the save ('file name') method to save the document

You can check the official website: https://python-docx.readthedocs.io/en/latest/

From docx import Documentdocument = Document () # insert first-level title document.add_heading ('ancient poetry', level=0) # insert title # add paragraph p = document.add_paragraph (''life is an arrival, we always think that the future is long, but the future is not long, we are always looking forward to tomorrow, neglecting today, we are always looking up at the sky, but forget to take the road under our feet.'' ,) # insert secondary heading document.add_heading ('Spring Night', level=1,) # insert paragraph document.add_paragraph ('timely rain knows the season, it should be born in spring.' , style='ListNumber') document.add_paragraph ('as the spring breeze creeps into the night, moisturizing everything is silent.' , style='ListNumber') document.add_paragraph ('the country road is as dark as the clouds, and the river boat fishing lamp is alone.' , style='ListNumber') document.add_paragraph ('in the morning I saw the safflower wet by the rain, and the branches of the flowers filled with clusters of Jinguancheng. , style='ListNumber') # Save document document.save ('article.docx') 2.Python read word document

To read an word document, you need to add a document path to Document () to open the document.

After opening the document, you can read the document according to your needs, for example, paragraphs reads the document paragraph, tables reads the document table set, etc.

Adding content to an existing document is the same as writing to a document, and finally, save the document through the save () method.

From docx import Documentdocument = Document ('. / article.docx') # read the contents of the word document line by line for paragraph in document.paragraphs: print (paragraph.text) document.add_paragraph ('Kung Hei Fat Choi', style='ListNumber') # Save the document document.save ('new_artical.docx') here, I believe you have a deeper understanding of "how to use Python to read and write word documents", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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