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

#python #word #write #read

How to read/write Word docx files in Python » grokonez
1.55 GEEK