2017-07-16 105 views
0

我試圖按照這個question在我的Excel中使用python和openpyxl包添加一些公式。用Python編寫公式到Excel Excel

該鏈接是我需要的任務。

但在此代碼:

for i, cellObj in enumerate(Sheet.columns[2], 1): 
    cellObj.value = '=IF($A${0}=$B${0}, "Match", "Mismatch")'.format(i) 

我拿在Sheet.columns[2]任何想法,爲什麼錯誤?我遵循完整的代碼。

我有python 2.7.13版本,如果這有助於這個錯誤。

**** **** 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: 'generator' object has no attribute 'getitem'

+0

您是否將'Sheet'命名爲變量? – PRMoureu

+0

@PRMoureu是''Sheet'簡單的問題鏈接'Sheet = wb.get_sheet_by_name('Sheet1')' – Mar

+0

您能否請添加確切的錯誤,並且代碼也可以幫助您,如果您可以更新您的帖子 – PRMoureu

回答