Python错误:ImportError: cannot import name get_column_letter 解决办法
因为python版本更新而遇到的错误,今天一大早又遇到了一次…
from openpyxl.cell import get_column_letter
import openpyxl
from openpyxl.cell import get_column_letter,column_index_from_letter
wb = openpyxl.load_workbook('example.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
get_column_letter(100)
>>> from openpyxl.cell import get_column_letter
Traceback (most recent call last):
File "<pyshell#166>", line 1, in <module>
from openpyxl.cell import get_column_letter
ImportError: cannot import name 'get_column_letter' from 'openpyxl.cell' (C:\Python37\lib\site-packages\openpyxl\cell\__init__.py)
这个错误的意思就是导入 get_column_letter方法失败,于是Google了一下发现,get_column_letter方法已经在Openpyxl 的3版本中重写了,如今想要用此方法需要从openpyxl.utils中导入,而非从openpyxl.cell。
因此正确的导入方式应该为:
from openpyxl.utils import get_column_letter