Python openpyxl模块说:AttributeError:' tuple'对象没有属性' upper'

时间:2015-03-10 20:45:58

标签: python tuples export-to-excel attributeerror openpyxl

安装了Python 3.4和模块jdcalopenpyxl

openpyxl库上尝试自己从Python读取和写入XLSX文件。我安装了jdcall模块和openpyxl模块。代码让我创建工作簿和工作表:

from openpyxl import Workbook
wb  = Workbook()
ws  = wb.active

但是,如果我尝试写这样的第一个单元格:

ws[ 1, 1]   = 'testing 1-2-3'

Python说:

C:\Wolf\Python Studies>database.py
Traceback (most recent call last):
 File "C:\Wolf\Python Studies\database.py", line 13, in <module>
   ws[ 1, 1]   = 'testing 1-2-3'
 File "C:\Python34\lib\site-packages\openpyxl-2.2.0b1-py3.4.egg\openpyxl\worksheet\worksheet.py", line 403, in __setitem__
   self[key].value = value
 File "C:\Python34\lib\site-packages\openpyxl-2.2.0b1-py3.4.egg\openpyxl\worksheet\worksheet.py", line 400, in __getitem__ <BR>
   return self._get_cell(key)
 File "C:\Python34\lib\site-packages\openpyxl-2.2.0b1-py3.4.egg\openpyxl\worksheet\worksheet.py", line 368, in _get_cell
   coordinate = coordinate.upper()
AttributeError: 'tuple' object has no attribute 'upper'

C:\Wolf\Python Studies>

知道我做错了吗?

1 个答案:

答案 0 :(得分:3)

单元格坐标应作为字符串提供

ws['A1'] = 'testing 1-2-3'

或者,如果要使用行索引和列索引,请使用ws.cell().value

ws.cell(row=1, column=1).value = 'testing 1-2-3'