Skip to content

Instantly share code, notes, and snippets.

@etienned
Last active November 21, 2022 13:56
Show Gist options
  • Save etienned/7539105 to your computer and use it in GitHub Desktop.
Save etienned/7539105 to your computer and use it in GitHub Desktop.
Simple function to extract text from MS XML Word document (.docx) without any dependencies.
try:
from xml.etree.cElementTree import XML
except ImportError:
from xml.etree.ElementTree import XML
import zipfile
"""
Module that extract text from MS XML Word document (.docx).
(Inspired by python-docx <https://github.com/mikemaccana/python-docx>)
"""
WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
PARA = WORD_NAMESPACE + 'p'
TEXT = WORD_NAMESPACE + 't'
def get_docx_text(path):
"""
Take the path of a docx file as argument, return the text in unicode.
"""
document = zipfile.ZipFile(path)
xml_content = document.read('word/document.xml')
document.close()
tree = XML(xml_content)
paragraphs = []
for paragraph in tree.getiterator(PARA):
texts = [node.text
for node in paragraph.getiterator(TEXT)
if node.text]
if texts:
paragraphs.append(''.join(texts))
return '\n\n'.join(paragraphs)
@susmithavj
Copy link

how to view the output of this program

@VijayaLakshmiArthanari
Copy link

I am glad to thanking you @etienned. This code was working very well. I am struggled because of this one hyperlink problem.

@kaleem7832
Copy link

how to use this script to convert word file to xml

@Maodourr
Copy link

It works great! Thanks

@Sreenathneelamana
Copy link

@etienned Is it possible to read "heading" style and "Normal" style separately ??

@multinucliated
Copy link

multinucliated commented Sep 4, 2020

Thanks a lot for this !! It saved a lot of resources for me.

Also, can you give me some direction or suggestion for extracting data from doc file ?

@AlanReviews
Copy link

This code is amazing! I really like how it is designed and works. Is it possible to have UTF-8 encoding in this program? Some special characters such as accented characters do not render nicely.

@wukuai
Copy link

wukuai commented Sep 30, 2020

Thanks! It do me a big favor!

@teng-pixiumdigital
Copy link

Thanks!

@AgnijDave
Copy link

Works like a charm! Thank You!

@PFython
Copy link

PFython commented Apr 23, 2021

Methods getchildren() and getiterator() of classes ElementTree and Element in the ElementTree module have been removed. They were deprecated in Python 3.2. Use iter(x) or list(x) instead of x.getchildren() and x.iter() or list(x.iter()) instead of x.getiterator(). (Contributed by Serhiy Storchaka in bpo-36543.)

@AgnijDave
Copy link

comment out everything in the function -> get_docx_text() and add these statements ->
#print(type(document))
#print(document.namelist(),'\n\n')
#print(document.printdir()) <> It's possible the 'word/document.xml' is not present in the meta data of that docx file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment