Reputation: 53
I'm a novice in python, but I'm trying to learn to make my life a bit easier. I have written a simple script that manipulates several cells in a sheet:
import openpyxl
wb = openpyxl.load_workbook(....)
wb.get_sheet_names()
ws1 = wb.get_sheet_by_name('Cmg 1')
ws1['Q2'] = '=WORKDAY(N2,1,K7:K63)'
ws1['R2'] = '=TEXT(Q2,"yyyymmdd")'
....
wb.save('Daily_updated.xlsx')
It works well for one sheet, but the problem is that I have an Excel with 50sheets in total, where half needs to be udpated, sheets don't have any pattern.
Since I know which sheets to update (by name), I was thinking of requesting names of all sheets and then define in script which sheets need to be updated as : "sheets to update" that the script would only update these XYZ sheets..
Can anyone help me how to incorporate that into my simple script? Thanks
Upvotes: 0
Views: 80
Reputation: 8077
If you know the names of the sheets that need to be updated, you can specify a list with these names and update as you wish:
import openpyxl
SHEETS_TO_UPDATE = ['Cmg 1', 'Cmg 2', 'Cmg 3', 'Cmg 4']
wb = openpyxl.load_workbook(....)
for sh in SHEETS_TO_UPDATE:
ws = wb.get_sheet_by_name(sh)
ws['Q2'] = '=WORKDAY(N2,1,K7:K63)'
ws['R2'] = '=TEXT(Q2,"yyyymmdd")'
....
wb.save('Daily_updated.xlsx')
Upvotes: 1