26

I am able to use openpyxl as an import in my code. But when I try to do the following:

from openpyxl.cell import get_column_letter 

I get the following error:

ImportError: cannot import name get_column_letter

I am using python 2.7. I have installed it using easy_install. Tried searching for this issue but couldn't find anything related to it.

CDspace
  • 2,639
  • 18
  • 30
  • 36
charsi
  • 399
  • 1
  • 5
  • 13
  • Are you using some `python package by the same name`? Please make sure that you are not importing any custom made python package/file by the name `get_column_letter` – cutteeth Apr 19 '16 at 14:38

6 Answers6

53

The function get_column_letter has been relocated in Openpyxl version 2.4 from openpyxl.cell to openpyxl.utils.

The current import is: from openpyxl.utils import get_column_letter

If you do not know which version the end-user has, you can use the following code:

try: 
    from openpyxl.cell import get_column_letter
except ImportError:
    from openpyxl.utils import get_column_letter
Abbas
  • 3,872
  • 6
  • 36
  • 63
1

from openpyxl.utils import get_column_letter

This is working for Python3 also.

darla_sud
  • 359
  • 1
  • 4
  • 15
0

I got the same problem and I reinstall the latest openpyxl using "python setup.py install". Then it works.

0
print(openpyxl.cell.get_column_letter(1)) # does not work …

You would rather use

print(openpyxl.utils.get_column_letter(1))
Stphane
  • 3,368
  • 5
  • 32
  • 47
shyed2001
  • 35
  • 6
0

Working in Python 3.8:

Once the library has been imported using

from openpyxl.utils import get_column_letter

the alphanumeric column value can be acquired by this approach:

print(get_column_letter(26))

or

column_variable = get_column_letter(26)

Sorry if this is too simplistic, it took me a while to realize the conversion from column index number to column letter is independent of any sheet that might be open, therefore, there's no need to use dot.methods for a worksheet or individual cell.

CraigS
  • 1
  • 1
-2

tl;dr for Python3

  • pip3 install Cython
  • pip3 install pandas


Neither of the other two solutions from Abbas or Jael Woo worked for me for Python3.

I ended up using apt-get install python3-pandas, but then pip3 install pandas failed because it said I needed Cython, as it does mention in the Pandas installation docs that it is an "optional dependency" anyways.

That being said, I ran pip3 install Cython and then ran pip3 install pandas, and it worked.


Note: Cython and Pandas installation took a while on Ubuntu (unsure of EC2's Ubuntu version) but seemed to be much quicker on Mac 10.11.5

EDIT: using apt-get to install Pandas yielded errors because apt-get installed an older version of Pandas. Once I installed/upgraded Pandas using pip3, the ImportErrors were gone.

Edit: if you care enough to downvote, try adding some constructive criticism to this answer in the form of a comment

Bryce
  • 248
  • 5
  • 16