Parse .docx in python 3

29,381

Solution 1

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.

Solution 2

you can solve your import problem by first uninstalling the existant installation and then install it with pip3. solved my problem with thisenter image description here

    pip uninstall python-docx
    pip3 install python-docx
Share:
29,381
thehoule64
Author by

thehoule64

Updated on October 12, 2020

Comments

  • thehoule64
    thehoule64 over 3 years

    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.

  • Chris22
    Chris22 almost 6 years
    Even though it appeared to be successfully installed, it isn't listed in my site-packages folder (I can't attach a screengrab in the comments) Collecting python-docx Collecting lxml>=2.3.2 (from python-docx) Downloading files.pythonhosted.org/packages/5c/4c/… 100% Installing collected packages: lxml, python-docx Successfully installed lxml-4.2.3 python-docx-0.8.6 Mac High sierra, python 3.7, Pycharm 2018. Could someone tell me why isn't it listed after successful install? Thanks.