问题描述
我尝试遵循以下问题在Excel中添加一些公式使用python和openpyxl软件包.
I try to follow this question to add some formula in my excel using python and openpyxl package.
该链接是我完成任务所需要的.
That link is what i need for my task.
但在此代码中:
for i, cellObj in enumerate(Sheet.columns[2], 1):
cellObj.value = '=IF($A${0}=$B${0}, "Match", "Mismatch")'.format(i)
我在Sheet.columns[2]
出现错误,知道为什么吗?我遵循完整的代码.
i take an error at Sheet.columns[2]
any idea why ? i follow the complete code.
我有python 2.7.13版本,如果此错误有帮助的话.
i have python 2.7.13 version if that helps for this error.
****更新****
****UPDATE****
完整代码:
import openpyxl
wb = openpyxl.load_workbook('test1.xlsx')
print wb.get_sheet_names()
Sheet = wb.worksheets[0]
for i, cellObj in enumerate(Sheet.columns[2], 1):
cellObj.value = '=IF($A${0}=$B${0}, "Match", "Mismatch")'.format(i)
错误消息:
for i, cellObj in enumerate(Sheet.columns[2], 1):
推荐答案
ws.columns
和ws.rows
是返回生成器的属性.但是openpyxl还支持对行和列进行切片和索引
ws.columns
and ws.rows
are properties that return generators. But openpyxl also supports slicing and indexing for rows and columns
因此,ws['C']
将在第三列中提供单元格的列表.
So, ws['C']
will give a list of the cells in the third column.
这篇关于使用Python错误将公式写入Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!