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

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.

Get paragraphs

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

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

Write docx file
Create Word document

We call docx.Document() function to get a new, blank Word Document object.

Save Word Document

When everything’s done, we must use save('filename.docx') Document’s method with filename to save the Document object to a file.

Add Paragraphs

We use add_paragraph() Document’s method to add a new paragraph and get a reference to this Paragraph object.

We can add text to the end of an existing Paragraph object using Paragraph’s add_run(text) method.

Result in grokonez.docx:


To make text styled, we add Run with text attributes.

Result in grokonez.docx:


These are some text attributes:
strike (strikethrough)
double_strike (double strikethrough)
all_caps (capital letters)
rtl (right-to-left)

Add Headings

We use Document’s add_heading(heading, i) method to add a paragraph with heading style with i argument from 0 to 9 for heading levels.


Add Line Breaks, Page Breaks

Instead of starting a new paragraph, we can add a line break using Run object add_break() method on the one that we want to have the break appear after.

Result in grokonez.docx:


We can also add a page break with add_break() method by passing the value docx.enum.text.WD_BREAK.PAGE as an argument to it.

Result in grokonez.docx:


Add Pictures

We can use Document object’s add_picture() method to add an image to the end of the document.

Result in grokonez.docx:


add_picture() method has optional width and height arguments.
If we don’t use them, the width and height will default to the normal size of the image.

Result in grokonez.docx:


Result in grokonez.docx:


By grokonez | January 31, 2019.

Related Posts

22 thoughts on “How to read/write Word docx files in Python”

  1. 87069 649134This is an outstanding write-up and I completely comprehend where your coming from within the third section. Perfect read, Ill regularly follow the other reads. 172528

  2. “Open cmd, then run:
    pip install python-docx

    Once the installation is successful…”

    I’m still pretty green, so this is not very clear. This line does not execute in the command prompt, nor does it execute in IDLE. I assume this is Windows and not Mac? What version of Python? If this was more clear, I’d be home free. Perhaps a hyperlink for “cmd” to clear things up?

    I know this sounds like the typical internet tantrum – it isn’t – but a lot of stuff that is understandably taken for granted by seasoned programmers is the difference between going and stopping for us newcomers. And I would hazard to guess that newcomers outnumber experienced folk when searching for answers online. It doesn’t take much to lock us up, but we’re learning.

  3. 674832 693076Oh my goodness! an remarkable write-up dude. Thank you Even so My business is experiencing issue with ur rss . Dont know why Unable to subscribe to it. Can there be anyone obtaining identical rss problem? Anybody who knows kindly respond. Thnkx 659631

  4. 482057 183500Youre so cool! I dont suppose Ive learn anything like this before. So nice to discover any person with some authentic thoughts on this subject. realy thank you for starting this up. this web site is something that is wanted on the internet, someone with a bit bit originality. helpful job for bringing something new to the internet! 362973

  5. 775128 901370Hosting a blog composing facility (in a broad sense) requires unlimited space. So I suggest you to discover such internet hosting (internet space provider) that give flexibility inside your internet space. 429857

  6. 132681 662951In case you tow a definite caravan nor van movie trailer your entire family pretty soon get exposed to the down sides towards preventing greatest securely region. awnings 993020

  7. 98355 751517I think other web site proprietors need to take this site as an model, very clean and excellent user friendly style and design, as nicely as the content. You are an expert in this topic! 741909

Got Something To Say:

Your email address will not be published. Required fields are marked *