Skip to main content

#docx2python

Extract docx headers, footers, text, properties, and images to a Python object.

The code is an expansion/contraction of python-docx2txt (Copyright (c) 2015 Ankush Shah). The original code is mostly gone, but some of the bones may still be here.

shared features:

  • extracts text from docx files
  • extracts images from docx files
  • no dependencies (docx2python requires pytest to test)

additions:

  • converts bullets and numbered lists to ascii with indentation
  • retains some structure of the original file (more below)
  • extracts document properties (creator, lastModifiedBy, etc.)
  • inserts image placeholders in text ('----image1.jpg----')
  • (optionally) retains font size, font color, bold, italics, and underscore as html
  • full test coverage

subtractions:

  • no command-line interface
  • will only work with later versions of Python

#Installation

pip install docx2python

#Use

from docx2python import docx2python

# extract docx content
docx2python('path/to/file.docx')

# extract docx content, write images to image_directory
docx2python('path/to/file.docx', 'path/to/image_directory')

# extract docx content with basic font styles converted to html
docx2python('path/to/file.docx', html=True)

Note on html feature:

  • font size, font color, bold, italics, and underline supported
  • every tag open in a paragraph will be closed in that paragraph (and, where appropriate, reopened in the next paragraph). If two subsequenct paragraphs are bold, they will be returned as <b>paragraph q</b>, <b>paragraph 2</b>. This is intentional to make each paragraph its own entity.
  • if you specify export_font_style=True, > and < in your docx text will be encoded as &gt; and &lt;

#Return Value Function docx2python returns an object with several attributes.

header - contents of the docx headers in the return format described herein

footer - contents of the docx footers in the return format described herein

body - contents of the docx in the return format described herein

document - header + body + footer

text - all docx text as one string, similar to what you'd get from python-docx2txt

tables - all docx text as simple html tables

properties - docx property names mapped to values (e.g., {"lastModifiedBy": "Shay Hill"})

images - image names mapped to images in binary format. Write to filesystem with

for name, image in result.images.items():
    with open(name, 'wb') as image_destination:
        write(image_destination, image)

#Return Format Some structure will be maintained. Text will be returned in a nested list, with paragraphs always at depth 4 (i.e., output.body[i][j][k][l] will be a paragraph).

If your docx has no tables, output.body will appear as one a table with all contents in one cell:

[  # document
    [  # table
        [  # row
            [  # cell
                "Paragraph 1",
                "Paragraph 2",
                "-- bulleted list",
                "-- continuing bulleted list",
                "1)  numbered list",
                "2)  continuing numbered list"
                "    a)  sublist",
                "        i)  sublist of sublist",
                "3)  keeps track of indention levels",
                "    a)  resets sublist counters"
            ]
        ]
     ]
 ]

Table cells will appear as table cells. Text outside tables will appear as table cells.

To preserve the even depth (text always at depth 4), nested tables will appear as new, top-level tables. This is clearer with an example:

#  docx structure

[  # document
    [  # table A
        [  # table A row
            [  # table A cell 1
                "paragraph in table A cell 1"
            ],
            [  # nested table B
                [  # table B row
                    [  # table B cell
                        "paragraph in table B"
                    ]
                ]
            ],
            [  # table A cell 2
                'paragraph in table A cell 2'
            ]
        ]
    ]
]

becomes ...

[  # document 
    [  # table A
        [  # row in table A
            [  # cell in table A
                "table A cell 1"
            ]
        ]
    ],
    [  # table B
        [  # row in table B
            [  # cell in table B
                "table B cell"
            ]
        ]
    ],
    [  # table C
        [  # row in table C
            [  # cell in table C
                "table A cell 2"
            ]
        ]
    ]
]

This ensures text appears

1) only once
2) in the order it appears on the docx
3) always at depth four (i.e., result.body[i][j][k][l] will be a string).

Release files for docx2python 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for docx2python 0.1
File Size Uploaded
docx2python-0.1.tar.gz 21.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for docx2python 0.1
File Interpreter ABI Platform
docx2python-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 39.8 kB

Release files / docx2python-0.1.tar.gz

Download URL docx2python-0.1.tar.gz
Size 21.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8d381bdc55799d521a9ac4c5e6156f8652d1ee120006a29927c2b415fa109dbb
BLAKE2b-256 checksum
How to use checksums
7dc217e21087a7bf0ec76c916ec97eae7b6e65bc8db7ca70abc20d93b4088962
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Release files / docx2python-0.1-py3-none-any.whl

Download URL docx2python-0.1-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
74cd8a787b442cd2d2c923b62ca8335c47c1c88e396b8cb280837a2f9618cfc8
BLAKE2b-256 checksum
How to use checksums
9592ae96875d51b23ae14a0bed597a56592c0ed7ead8d09f865dc6dcf4a6e0db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Release history Release notifications | RSS feed

3.7.1

2 release files

3.6.2

2 release files

3.6.1

2 release files

3.5.0

2 release files

3.4.1

2 release files

3.4.0

2 release files

3.3.1

2 release files

3.3.0

2 release files

3.2.2

2 release files

3.1.0

2 release files

3.0.2

2 release files

3.0.0

2 release files

2.8.0

2 release files

2.7.3

2 release files

2.7.2

2 release files

2.6.3

2 release files

2.6.0

2 release files

2.5.1

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.27.1

2 release files

1.27

2 release files

1.26

2 release files

1.25

2 release files

1.24

2 release files

1.23

2 release files

1.22

2 release files

1.21

2 release files

1.19

2 release files

1.18

2 release files

1.17

2 release files

1.16

2 release files

1.15

2 release files

1.14

2 release files

1.13

2 release files

1.12

2 release files

1.11

2 release files

1.2

3 release files

1.1

3 release files

1.0

2 release files

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page