how to write to a new cell in python using openpyxl

后端 未结 1 891
梦如初夏
梦如初夏 2020-12-03 11:35

I wrote code which opens an excel file and iterates through each row and passes the value to another function.

import openpyxl
wb = load_workbook(filename=\'         


        
相关标签:
1条回答
  • 2020-12-03 11:57

    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=' is optional here
    

    This will set Cells A1, B2 and C3 to 1, 2 and 3 respectively (three different ways of setting cell values in a worksheet).

    The second method (specifying row and column) is most useful for your situation:

    import openpyxl
    wb = load_workbook(filename='xxxxx.xlsx')
    for ws in wb.worksheets:
        for index, row in enumerate(ws.rows, start=1):
            print row
            x1 = ucr(row[0].value)
            ws.cell(row=index, column=2).value = x1
    
    0 讨论(0)
提交回复
热议问题