2

Is there any good library that can help to parse a .docx file in a serial manner?

I am in search of a library that can allow me to parse a docx file in following fashion:

d = open("input.docx","rb")
parsed = library.parse(d, output="json")
print(parsed)

and I expect output as:

[
   {
      "type":"page",
      "children":[
         {
            "type":"header",
            "children":[
               {
                  "type":"line",
                  "children":[
                     {
                        "text":"",
                        "fontSize":11
                     }
                  ]
               }
            ]
         },
         {
            "type":"content",
            "active":true,
            "children":[
               {
                  "type":"line",
                  "children":[
                     {
                        "text":"",
                        "fontSize":11
                     }
                  ]
               }
            ]
         },
         {
            "type":"footer",
            "children":[
               {
                  "type":"line",
                  "children":[
                     {
                        "text":"",
                        "fontSize":11
                     }
                  ]
               }
            ]
         }
      ],
      "marginTop":1,
      "marginLeft":1,
      "marginRight":1,
      "orientation":"portrait",
      "marginBottom":1
   }
]

python and javascript are preferred as these are the popular languages.

1
  • "python and javascript are preferred as these are the popular languages." Defenetly not popular languages. Gues what you really wanted to say is they are your favorite languages.
    – convert
    Jun 4, 2023 at 17:00

2 Answers 2

1

Here are some libraries for python that you can use:

  1. https://github.com/Untether-Tech/Simplify-Docx/
  2. https://github.com/jdthorpe/python-docx/
  3. https://github.com/CenterForOpenScience/pydocx/
1

I can recommend docx2python (https://github.com/ShayHill/docx2python), which I somehow haven't seen mentioned anywhere else on the internet :shrug:.

It only extracts document content and basic text formatting (as html), but has some really excellent features -- like handling hyperlinks, merging runs with the same formatting, or allowing you to dump images to a folder -- that I haven't seen in any other python library.

It represents content as nested lists of 'tables' that are reasonable enough to work on:

from docx2python import docx2python
from docx2python.docx_text import TablesList
# TablesList is just: List[List[List[List[str]]]]

with docx2python('my_file.docx', html=True) as document:
    header = document.header
    body = document.body
    body_runs = document.body_runs # separates paragraph into a list of runs
    footer = document.footer
    image_dict: Dict[str, bytes] = document.images

# if you don't have any tables, there will only be one cell
for table in body:
    for row in table:
        for cell in row:
            for line in cell:
                do_stuff()

for table in body_runs:
    for row in table:
        for cell in row:
            for line in cell:
                for run in line:
                    do_stuff()

# you can also just get all the text as one string, but this will make some parsing more complicated:
text = docx2python('my_file').text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.