Docx missing attributes - python

I'm trying to do anything considering word document using docx library in python. The problem is, whatever I import, I get error message about 'no attribute'. For eample - Document
from docx import Document
gives output
cannot import name Document
and any try to use Document ends with error
AttributeError: 'module' object has no attribute 'Document'
Any syntax seems to be correct. I'm using docx module version 0.2.4 .
Thanks for all help.

I had a similar problem. I had used pip install docx in my virtual environment. The fix was to pip uninstall docx and then pip install python-docx.

from official documentation
python-docx versions 0.3.0 and later are not API-compatible with prior versions.
so, try to update package or use old api

are you sure you properly installed docx? Because it seems it is not installed.
You should try using pip or easy_install to install it properly.
http://python-docx.readthedocs.org/en/latest/user/install.html
For example, I never installed it and I get this following code error if I try to import it which is pretty much what you get :
Traceback (most recent call last):
File "docx.py", line 1, in <module>
from docx import Document
File "/Users/xxxx/Documents/Python/docx.py", line 1, in <module>
from docx import Document
ImportError: cannot import name Document

Related

Python docx module ModuleNotFoundError

I am a windows 7 user with MS-Office 2016 and python 3.6.
I recently found out that you can write word documents with python.
I looked on how to do that
I installed the docx module:
pip install docx
But whenever i try to import the docx module i get this error:
>>> from docx import Document
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Huzefa\AppData\Local\Programs\Py
in <module>
from exceptions import PendingDeprecationWarn
ModuleNotFoundError: No module named 'exceptions'
I have even tried this but it wont work:
pip install exceptions
I get this error:
ERROR: Could not find a version that satisfies the requirement exceptions (from versions: none)
ERROR: No matching distribution found for exceptions
I thought that maybe this is happening because docx is a python-2 module but it is not. I found it out here
so can someone tell me what i am doing wrong?
I suggest that you can try like this -
$ pip install --pre python-docx
Or you can also try sudo command -
$ sudo pip install --pre python-docx
After that try -
from docx import Document
From here you can find [https://python-docx.readthedocs.io/en/latest/user/install.html][1]
try using:
pip install python-docx
It worked for me!
The problem here is that the name for installation and usage are different:
To install: pip install python-docx
To use: from docx import Document
Install with the correct name (i.e. python-docx) and it shall work.

No module named 'PyPDF2' When Trying to Import

I am attempting to Import PyPDF2 in order to read a PDF file and parse through it. I am using a Raspberry Pi, and have installed PyPDF2 using the command pip install PyPDF2. However, when I try importing it, I am getting ModuleNotFoundError.
I think it may have installed it into the wrong location. When I do python -V it says I am using version 2.7.16. But the error states it's trying to look into the python3 folder?
I am attempting to import it using the line import PyPDF2
The error I'm getting is:
Traceback (most recent call last):
File "/home/pi/SqlDatabase.py", line 5, in <module>
import PyPDF2
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 305, in _custom_import
module = self._original_import(*args, **kw)
ModuleNotFoundError: No module named 'PyPDF2'
Any idea of how I can install PyPDF into the correct directory, or perhaps a different solution?
If you are starting the program with python3 (e.g. see if the first line of the file has #!/usr/bin/python3 or similar), you need to install the library with pip3.
It seems that you have not installed PyPDF2 module in your device.
Execute following code in your terminal:
pip install PyPDF2
or
pip3 install PyPDF2
I think this will solve your problem. If this does not solve your problem, then there may be problem in your directory of python.
I have also faced the same issue. Note that Python is a case-sensitive language.
While using the import command use PyPDF2 and not pyPDF2
To install:
pipenv install PyPDF2
To import:
import PyPDF2

ImportError: No module named dns.exception

I am trying to use the dnspython in my code but when it gets to the import statement, I get the following error:
>>> import DNS.resolver
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\python27\lib\site-packages\DNS\resolver.py", line 31, in <module>
import dns.exception
ImportError: No module named dns.exception
I have tried installing with pip, easy_install and manually from the git repo but nothing seems to do the trick. Any ideas??
Code:
import DNS.resolver
if __name__ == "__main__":
domain = 'hotmail.com'
for x in dns.resolver.query(domain, 'MX'):
print x.to_text()
Another thing you can do to fix problems with this package is to install python-dnspython again using Synaptic. (Whether no having previous problems about duplicity)
Sometimes if you try to install this package using pip could appear some problems like that:
Command python setup.py egg_info failed with error code 1 in
/tmp/pip_build_root/dnspython Storing debug log for failure in
/root/.pip/pip.log
Using Synaptic cleans the old files and install the new ones, from that package. I know this issue is solved but I wanted to add more information about this :)
That's because package and module names are lowercase (see PEP 8). This works just fine :
import dns.resolver
import dns.exception
You should also be careful that none of your own *.py filename conflicts with the dns package. Your *.py file should not be named dns.py. Pay also attention to the *.pyc files.
First, your code should be : import dns.resolver.
There seems to be an issue installing dnspython using pip, you should clone the dnspython repository using git and run the file setup.py as follows:
git clone https://github.com/rthalley/dnspython
cd dnspython\
python setup.py install
If you don't have git installed on your machine, you can just download the repository manually .

When import docx in python3.3 I have error ImportError: No module named 'exceptions'

when I import docx I have this error:
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/docx-0.2.4-py3.3.egg/docx.py", line 30, in <module>
from exceptions import PendingDeprecationWarning
ImportError: No module named 'exceptions'
How to fix this error (python3.3, docx 0.2.4)?
If you are using python 3x don't do pip install docx instead go for
pip install python-docx
It is compatible with python 3.x
Official Documentation available here: https://pypi.org/project/python-docx/
When want to use import docx, be sure to install python-docx, not docx.You can install the module by running pip install python-docx.
The installation name docx is for a different module
However,
when you are going to import the python-docx module,
you’ll need to run
import docx, not import python-docx.
if still you want to use docx module then:
First of all, you will need to make sure that the docx module is installed.
If not then simply run pip install docx.
If it shows '*requirement already satisfied*'
then the solution is :
Go to the library to find docx.py file,
you'll need to go to directory where you installed python then \Lib\site-packages\ and find docx.py file
Open docx.py file in text editor and find this code
from exceptions import PendingDeprecationWarning
Replace the above code with
try:
from exceptions import PendingDeprecationWarning
except ImportError:
pass
Save the file
Now you can run import docx module in Python 3.x without any problem
Uninstall docx module with pip uninstall docx
Download python_docx-0.8.6-py2.py3-none-any.whl file from http://www.lfd.uci.edu/~gohlke/pythonlibs/
Run pip install python_docx-0.8.6-py2.py3-none-any.whl to reinstall docx.
This solved the above import error smoothly for me.
If you're using python 3.x, Make sure you have both python-docx & docx installed.
Installing python-docx :
pip install python-docx
Installing docx :
pip install docx
You may be install docx, not python-docx
You can see this for install python-docx
http://python-docx.readthedocs.io/en/latest/user/install.html#install
In Python 3 exceptions module was removed and all standard exceptions were moved to builtin module. Thus meaning that there is no more need to do explicit import of any standard exceptions.
copied from
pip install python-docx
this worked for me, try installing with admin mode
The problem, as was noted previously in comments, is the docx module was not compatible with Python 3. It was fixed in this pull-request on github: https://github.com/mikemaccana/python-docx/pull/67
Since the exception is now built-in, the solution is to not import it.
docx.py
## -27,7 +27,12 ##
except ImportError:
TAGS = {}
-from exceptions import PendingDeprecationWarning
+# Handle PendingDeprecationWarning causing an ImportError if using Python 3
+try:
+ from exceptions import PendingDeprecationWarning
+except ImportError:
+ pass
+
from warnings import warn
import logging
I had the same problem, but pip install python-docx worked for me, I'm using python 3.7.1
You need to make it work with python3.
sudo pip3 install python-docx
This installation worked for me in Python3 without any further additions.
python3
>> import docx
PS: Note that the 'pip install python-docx' or apt-get python3-docx are not useful.
I had the same problem, after installing docx module, got a bunch of errors regarding docx, .oxml and lxml...seems that for my case the package was installed in this folder:
C:\Program Files\Python3.7\Lib\site-packages
and I moved it one step back, to:
C:\Program Files\Python3.7\Lib
and this solved the issue.
Had faced similar issue and found a work around have posted answer aswell under similar question find its link :https://stackoverflow.com/a/74609166/17385292
python imports exceptions module by itself
comment out import line. it worked for me.

Parse .docx in python 3

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.
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.
you can solve your import problem by first uninstalling the existant installation and then install it with pip3. solved my problem with this
pip uninstall python-docx
pip3 install python-docx
Use Command- sudo pip install --pre python-docx for the latest version of python-docx.

Categories