'Add the same value in multiple cells when parsing data to a spreadsheet with openpyxl
Can I add the same value in two cells at the same line of code
I want to put "Run No." in the cell ["A1"] and ["B1"].
I could do this
sheet["A1"] = "Run No."
sheet["B1"] = "Run No."
But I need to add this in many cells. I was thinking if I can create a list like this list = ["A1","B1"]
sheet[list] = "Run No."
Solution 1:[1]
Just use iter_rows, you can specify the start and end rows and start and end columns then just for each cell in the range set the value.
If it is just A1 and B1 then set the values as
first_row = 1
last_row = 1
start_col = 1
end_col = 2
from openpyxl import load_workbook
filename = r"./book1.xlsx"
wb = load_workbook(filename)
ws = wb["Sheet1"]
# Example Row 1 to Row 10 and Column A to Column T will be filled
first_row = 1
last_row = 10
start_col = 1
end_col = 20
for row in ws.iter_rows(min_row=first_row, max_row=last_row, min_col=start_col, max_col=end_col):
for cell in row:
cell.value = 'Run No.'
wb.save(filename)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | moken |