[Automation] Read a Word document with Python

Read a Word document in Python

There are various documents in the office such as contracts and reports, but I think the mainstream is [Word documents].

When automating operations, there are many cases where you want to automate the creation and reading of Word documents. In fact, I also use Python to automate the creation of consignment contracts that I create every three months.

In this article, I will explain how to ** read Word documents with Python ** using a library called python-docx. (Next time, I will introduce how to create and replace Word documents.)

Reading Word documents

python-docx is not a standard library. Even Anaconda is not included by default, so let's install it first.

pip install python-docx

Import the library after installation. Please note that when importing, it is ** docx **, not python-docx.

python


import docx

Then read the Word document and create the object. Here, the following document called "test.docx" is read. test_docx.png

python


document = docx.Document("test.docx")

This document object has a list called paragraphs and a list called tables.

paragraphs are paragraphs in the text, and tables are tables. tables has rows as a list of rows, and rows has columns (cells) as a list called cells. If you want to get the text, refer to the attribute called text.

In other words, it has such a structure. python-docx.png

python


for paragraph in word.paragraphs:
    print(paragraph.text)

Execution result


This is the first paragraph.

This is the second paragraph.

python


for table in document.tables:
    for row in table.rows:
        for cell in row.cells:
            print(cell.text)

Execution result


Here is one row of the table/1 row
Here is one row of the table/2 rows
Here are two rows of the table/1 row
Here are two rows of the table/2 rows

important point

Unfortunately, python-docx can't read ** footnotes **.

If you want to operate including footnotes, you may need to consider another method.

Recommended Posts

[Automation] Read a Word document with Python
[Automation] Read mail (msg file) with Python
How to read a CSV file with Python 2/3
Read line by line from a file with Python
Read csv with python pandas
Make a fortune with Python
Create a directory with python
Document Python code with Doxygen
Read json data with python
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
[Automation] Extract Outlook appointments with Python
[Automation with python! ] Part 1: Setting file
Let's make a GUI with python.
Solve ABC166 A ~ D with Python
[Automation] Send Outlook email with Python
Create a virtual environment with Python!
I made a fortune with Python.
Building a virtual environment with Python 3
Solve ABC168 A ~ C with Python
[python] Read information with Redmine API
Make a recommender system with python
Solve ABC162 A ~ C with Python
Read files in parallel with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Let's make a graph with python! !!
[Automation with python! ] Part 2: File operation
Read fbx from python with cinema4d
[Python] Inherit a class with class variables
I made a daemon with Python
Write a batch script with Python3.5 ~
I made a library to easily read config files with Python
Programming with Python / JavaScript / VBScript inline scripting in Automation Anywhere A 2019
[Python] Read a csv file with a large data size using a generator
[Pyenv] Building a python environment with ubuntu 16.04
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Create a Python function decorator with Class
[Python] A program that creates stairs with #
Building a Python3 environment with Amazon Linux2
Let's make a shiritori game with Python
Install Python as a Framework with pyenv
Build a blockchain with Python ① Create a class
[Automation] Manipulate mouse and keyboard with Python
Read CSV file with python (Download & parse CSV file)
Add a Python data source with Redash
Create a dummy image with Python + PIL.
I made a character counter with Python
[Python] Drawing a swirl pattern with turtle
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
A memo with Python2.7 and Python3 on CentOS
Building a Python 3.6 environment with Windows + PowerShell
Map rent information on a map with python
Search the maze with the python A * algorithm
[C] [python] Read with AquesTalk on Linux
HTML document your Python program with Sphinx
Daemonize a Python web app with Supervisor
Let's read the RINEX file with Python ①
Let's make a voice slowly with Python