I'm trying to create variables inside a loop, then assign a value to them in the same loop iteration. My code is failing, I can see why, but cannot figure out how to fix it. I'm using Python 3.6 with current versions of all imported packages. Here's my code:
import openpyxl
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
for month in months:
filepath = 'C:\codes\Furnace_time_log\\2019\\' + month + '_2019.xlsx'
workbook_name = month+'_wb'
workbook_name = openpyxl.load_workbook(filepath)
sheet_name = month+'_sheet'
sheet_name = workbook_name.get_sheet_by_name('Sheet1')
What I want out of this is 12 openpyxl sheet objects named "January_sheet", "February_sheet", ...
Earlier in my code I have the following sample which does exactly what it's supposed to. This is what I'm basing the logic of my erroneous code on:
head_wb = openpyxl.load_workbook('C:\codes\Furnace_time_log\head.xlsx')
head_sheet = head_wb.get_sheet_by_name('Sheet 1')
I'm working with an Excel file where every row contains a date and a lot of other useful information. If row x's date is in January, I need to place the adjacent data in a separate XLSX document ("January_2019.xlsx"), which I can edit via the January_sheet object.
Any advice on how accomplish this? Also, I am not concerned with performance. This code only has to run once on the full data set. I realize there's probably a more efficient way of doing this than concurrently opening 12 openpyxl workbooks and sheets. That being said, any better ways of accomplishing this are welcome.