I'd ensure all your packages are up to date first; openpyxl and pillow.
Prima facie it would appear that pillow isn't installed
If you add the line after the openpyxl import
print(openpyxl.drawing.image.PILImage)
it should return the path where the PIL package resides. If it returns False then openpyxl is not finding it.
Also I'd expect you will get an error on line
img.anchor('A1')
the correct syntax is
img.anchor = 'A1'
see openpyxl docs openpyxl.drawing.image module
Answer from moken on Stack OverflowPython cannot import openpyxl - Stack Overflow
ImportError: cannot import name 'coordinate_from_string' from 'openpyxl.utils'
python - error> cannot import name 'load_workbook' from partially initialized module 'openpyxl' - Stack Overflow
python - Cannot import openpyxl properly - Stack Overflow
I had the same problem solved using instead of pip or easy install one of the following commands :
sudo apt-get install python-openpyxl
sudo apt-get install python3-openpyxl
The sudo command also works better for other packages.
While not quite what you ran into here (since you state that you are using python 2.7), for those who run into this issue and are using python 3, you may be unintentionally installing to python 2 instead. To force the install to python 3 (instead of 2) use pip3 instead.
See this thread for more info: No module named 'openpyxl' - Python 3.4 - Ubuntu
I am trying to install openpyxl in pycharm using pip install openpyxl, however when I import the openpyxl module, an error occurs.
I am using Pycharm on Ubuntu tho.
What I typed in terminal to install openpyxl:
pip install openpyxl
What returned:
Traceback (most recent call last):
File "/home/sheikhmugees/PycharmProjects/Python with Mosh/venv/bin/pip", line 7, in <module>
from pip._internal.cli.main import main
File "/home/sheikhmugees/PycharmProjects/Python with Mosh/venv/lib/python3.8/site-packages/pip/_internal/cli/main.py", line 9, in <module>
from pip._internal.cli.autocompletion import autocomplete
File "/home/sheikhmugees/PycharmProjects/Python with Mosh/venv/lib/python3.8/site-packages/pip/_internal/cli/autocompletion.py", line 10, in <module>
from pip._internal.cli.main_parser import create_main_parser
File "/home/sheikhmugees/PycharmProjects/Python with Mosh/venv/lib/python3.8/site-packages/pip/_internal/cli/main_parser.py", line 8, in <module>
from pip._internal.cli import cmdoptions
File "/home/sheikhmugees/PycharmProjects/Python with Mosh/venv/lib/python3.8/site-packages/pip/_internal/cli/cmdoptions.py", line 23, in <module>
from pip._internal.cli.parser import ConfigOptionParser
File "/home/sheikhmugees/PycharmProjects/Python with Mosh/venv/lib/python3.8/site-packages/pip/_internal/cli/parser.py", line 12, in <module>
from pip._internal.configuration import Configuration, ConfigurationError
File "/home/sheikhmugees/PycharmProjects/Python with Mosh/venv/lib/python3.8/site-packages/pip/_internal/configuration.py", line 26, in <module>
from pip._internal.utils.logging import getLogger
File "/home/sheikhmugees/PycharmProjects/Python with Mosh/venv/lib/python3.8/site-packages/pip/_internal/utils/logging.py", line 13, in <module>
from pip._internal.utils.misc import ensure_dir
File "/home/sheikhmugees/PycharmProjects/Python with Mosh/venv/lib/python3.8/site-packages/pip/_internal/utils/misc.py", line 40, in <module>
from pip._internal.locations import get_major_minor_version, site_packages, user_site
File "/home/sheikhmugees/PycharmProjects/Python with Mosh/venv/lib/python3.8/site-packages/pip/_internal/locations/__init__.py", line 14, in <module>
from . import _distutils, _sysconfig
File "/home/sheikhmugees/PycharmProjects/Python with Mosh/venv/lib/python3.8/site-packages/pip/_internal/locations/_distutils.py", line 9, in <module>
from distutils.cmd import Command as DistutilsCommand
ModuleNotFoundError: No module named 'distutils.cmd'
And here's my code:
import openpyxl as xl
wb = xl.load_workbook('translations.xlsx')
sheet = wb['Sheet1']
cell = sheet['a1']
print(cell)
and this is what comes when it runs:
File "/home/sheikhmugees/PycharmProjects/Python with Mosh/main.py", line 1, in <module>
import openpyxl as xl
ModuleNotFoundError: No module named 'openpyxl'I think you want:
from openpyxl import workbook # not Workbook
Note the capitalization of the name here.
I answer your second problem, because I found the solution (as though the cause of the first one is the same).
I think the problem is caused because the version that you have installed on your Ubuntu is not the latest version (1.5.7 at the moment). And the official documentation is based on the latest one.
For example the version of openpyxl provided on my Ubuntu 11.10 is not the latest, but 1.5.3, and if you use this syntax (taken from here: https://bitbucket.org/ericgazoni/openpyxl/wiki/Home), the commands work:
from openpyl.workbook import Workbook
for the Workbook and for load_workbook:
from openpyxl.reader.excel import load_workbook
But you can also install the latest one with easy_install:
$ sudo easy_install openpyxl
And to install easy_install, read this answer: https://askubuntu.com/questions/27519/can-i-use-easy-install