How to read/write Word docx files in Python
Python
82
word
2
read
6
write
5
Male avatar

loveprogramming viết ngày 16/05/2021

https://grokonez.com/python/how-to-read-write-word-docx-files-in-python-docx-module

How to read/write Word docx files in Python

In this tutorial, we're gonna look at way to use python-docx module to read, write Word docx files in Python program.

Word documents

Word .docx file has more structures than plain text. With python-docx module, we have 3 different data types:

  • a Document object for entire document.
  • Paragraph objects for the paragraphs inside Document object.
  • Each Paragraph object contains a list of Run objects.

read-write-word-docx-files-in-python-docx-module-docx-file

Read/Write Word docx files in Python

Install python-docx module

Open cmd, then run:
pip install python-docx

Once the installation is successful, we can see docx folder at Python\Python[version]\Lib\site-packages.
(In this tutorial, we use python-docx 0.8.10)

Now we can import the module by running import docx.

Read docx file

Open file

We call docx.Document() function and pass the filename to open a docx file under a Document object.


>>> import docx
>>> gkzDoc = docx.Document('grokonez.docx')

Get paragraphs

Document object has paragraphs attribute that is a list of Paragraph objects.


>>> gkzDoc = docx.Document('grokonez.docx')

>>> len(gkzDoc.paragraphs)
4
>>> gkzDoc.paragraphs[0].text
'JavaSampleApproach.com was the predecessor website to grokonez.com.'
>>> gkzDoc.paragraphs[1].text
'In this brandnew site, we don\u2019t only focus on Java & Javascript Technology but also approach to other technologies & frameworks, other fields of computer science such as Machine Learning and Testing. All of them will come to you in simple, feasible, practical and integrative ways. Then you will feel the connection of everything.'
>>> gkzDoc.paragraphs[2].text
'What does grokonez mean?'
>>> gkzDoc.paragraphs[3].text
'Well, grokonez is derived from the words grok and konez.'

Get full-text

To get full-text of the document, we will:

  • open the Word document
  • loop over all Paragraph objects and then appends their text

More at:

https://grokonez.com/python/how-to-read-write-word-docx-files-in-python-docx-module

How to read/write Word docx files in Python

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

545 bài viết.
97 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 11 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 9 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 9 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/kotlin/kotlinreadwritecsvfileexample How to read/write CSV file in Kotlin In this tutorial, we're gonna look at examples tha...
loveprogramming viết 5 tháng trước
0 0
Male avatar
0 0
https://grokonez.com/python/howtoreadwriteexcelfilesinpythonopenpyxlmodule How to read/write Excel files in Python In this tutorial, we're gonna...
loveprogramming viết 4 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
545 bài viết.
97 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!