2

I have installed openpyxl in ubuntu.
now I am running the openpyxl with xlsx files.

While importing the module, it gives me the following error.

from openpyxl import Workbook
ImportError: cannot import name Workbook

Can anyone knows what I have to do to solve the problem?

sam
  • 18,509
  • 24
  • 83
  • 116

3 Answers3

6

I think you want:

from openpyxl import workbook # not Workbook

Note the capitalization of the name here.

alexgolec
  • 26,898
  • 33
  • 107
  • 159
  • are you explicitly importing load_workbook yourself? if not, there might be some dependency in the openpyxl module you don't have installed. – alexgolec Mar 09 '12 at 18:02
3

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

Community
  • 1
  • 1
franzlorenzon
  • 5,845
  • 6
  • 36
  • 58
  • This seemed to work for me on Ubuntu. Use: sudo pip show pip, to see where the modules are. Then I was able to find the workbook.py file in the workbook folder. So I changed my local code of: from openpyxl import Workbook, to the code on ubuntu to: from openpyxl.workbook import workbook. – Robert Campbell Jan 31 '23 at 19:33
0

Actually latest version openpyxl not working well for load_workbook for python2.7 version . so just uninstall openpyxl by command --> pip uninstall openpyxl .

then reinstall openpyxl by giving providing version of same, works for me

pip install openpyxl=2.5.3 works

Abhishek
  • 89
  • 1
  • 4