close

how to write to a new cell in python using openpyxl

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about how to write to a new cell in python using openpyxl in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

how to write to a new cell in python using openpyxl?

  1. how to write to a new cell in python using openpyxl?

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

  2. write to a new cell in python using openpyxl

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

Method 1

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

This will set Cells A1 and B2 to 1 and 2 respectively (two 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

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read