使用Python错误将公式写入Excel [英] Write formula to Excel with Python error

查看:81
本文介绍了使用Python错误将公式写入Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试遵循以下问题在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):

TypeError:生成器"对象没有属性" getitem "

推荐答案

ws.columnsws.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆