Parse .docx in python 3

前提是你 提交于 2019-12-23 10:08:29

问题


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.


回答1:


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

$ pip install --pre 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 --pre 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.




回答2:


Use Command- sudo pip install --pre python-docx for the latest version of python-docx.



来源:https://stackoverflow.com/questions/21667719/parse-docx-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!