excel - how to write to a new cell in python using openpyxl -
i wrote code opens excel file , iterates through each row , passes value function.
import openpyxl wb = load_workbook(filename='c:\users\xxxxx') ws in wb.worksheets: row in ws.rows: print row x1=ucr(row[0].value) row[1].value=x1 # having error @ point
i getting following error when tried run file.
typeerror: indexerror: tuple index out of range
can write returned value x1
row[1]
column. possible write excel (i.e using row[1]
) instead of accessing single cells ws.['c1']=x1
try this:
import openpyxl wb = load_workbook(filename='xxxx.xlsx') ws = wb.worksheets[0] ws['a1'] = 1 ws.cell(row=2, column=2).value = 2 ws.cell(coordinate="c3").value = 3 # 'coordinate=' optional here
this set cells a1, b2 , c3 1, 2 , 3 respectively (three different ways of setting cell values in worksheet).
the second method (specifying row , column) useful situation:
import openpyxl wb = load_workbook(filename='xxxxx.xlsx') ws in wb.worksheets: index, row in enumerate(ws.rows, start=1): print row x1 = ucr(row[0].value) ws.cell(row=index, column=2).value = x1
Comments
Post a Comment