Parse .docx in python 3

thehoule64 picture thehoule64 · Feb 10, 2014 · Viewed 24.2k times · Source

I am currently writing a python 3 program that parses through certain docx files and extracts the text and images from them. I have been trying to use docx but it will not import into my program. I have installed lxml, Pillow, and python-docx yet it does not import. When I try to use python-docx from the terminal I cannot use example-extracttext.py or example-makedocument.py which brings me to believe that the installation didn't run properly. Is there a way I can check if this installed correctly or is there a way to get this working properly so I can import it into my project? I am on Ubuntu 13.10.

Answer

scanny picture scanny · Feb 10, 2014

I recommend you try the latest version of python-docx which is installed like this:

$ pip install python-docx

Documentation is available here: http://python-docx.readthedocs.org/

Installation should result in a message that looks successful. It's possible you'll need to install using sudo to temporarily assume root privileges:

$ sudo pip install python-docx

After installation you should be able to do the following in the Python interpreter:

>>> from docx import Document
>>>

If instead you get something like this, the install didn't go properly:

>>> from docx import Document
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named docx

As you can provide more feedback on your attempts I can elaborate the answer.

Note that after v0.2.x the python-docx package was rewritten. The API of v0.3.x+ is different as well as the package name and repository location. All further development will be on the new version. If you're just starting out with the package going with the latest is probably a good idea as the old one will just be receiving legacy support going forward.

Also, the Python 3 support was added with v0.3.0. Prior versions are not Python 3 compatible.