'Openpyxl does not iterate through worksheets as expected, how can I "FORCE!!!!" it to do as expected?
I have been provided with two spreadsheets with about 14 to 20 worksheets in each. I have been asked to compare the values in several sheets in one with sheets in the other file and write differences, if any, into a third file. I am having difficulty changing worksheets away from the first worksheet in any of the files. I have tried many things, this is not a new issue for me. I have tried the simplistic answers that I have found online, e.g. ws = wb.get_sheet_by_name(name = 'Sheet1') or ws = wb.activate or ws = wb['sheet1'] none of which work. I am at a loss to know why. I have looked at many, many examples; they are all, or mostly, this simplistic type.
I was expecting the sheet to change to the one named and when I iterated through rows and columns that the values would change for each sheet. I have been astounded to find that such a simple thing does not work. As an example the following code is one of my loops;
#Get Indicator numbers and descriptions from Trend Document. Write to Diff file.
for t in range(row_number, market_trend_maxrow+1):
ws3.iter_rows(min_row=row_number, max_row=market_trend_maxrow, min_col=1, max_col=2, values_only=False)
a_cell_value = ws3.cell(row=row_number, column=1).value
b_cell_value = ws3.cell(row=row_number, column=2).value
#print("Indicator: ", a_cell_value, "; ", b_cell_value)
ws10 = wb10.active
ws10['A' + str(row_number)] = a_cell_value
#print('\nrow number = ', row_number)
ws10['B' + str(row_number)] = b_cell_value
ws3 = wb1.active
row_number = row_number + 1
Output from the above loop;
…
row number = 208
Indicator: None ; None
row number = 209
Indicator: None ; None
row number = 210
Indicator: None ; None
…
I tried to iterate through the sheets in the workbook with a loop, for example;
for sheet in wb1:
if sheet.title == ws1.title:
ws1 = wb1.activeprint("\n\nWorksheet 'Overview Indicators' was found, working on it!")
print (datetime.now())
However this doesn't allow the code to access ws1 as I expected.
Additionally, I have opened the original spreadsheet multiple times to check output results. Output is only from the worksheet that was active when the file was last saved. I can change the output by changing the active worksheet, saving the file and then rerun my python code. Not really acceptable at all. It is a very frustrating issue, I have never been able to get this to work at all and I think all the examples I have seen are simplistic and using similar code to the above. For example;
for sheet in workbook.worksheets:
print sheet
I reasoned that if the worksheet equalled the one I was interested in I could select it and begin to work on it, however, the data returned was always from the active sheet when the file was last saved. Very frustrating conundrum! Does anyone have some wisdom/example that can definitively show to change from sheet to sheet in a excel file under examination? Thanks in advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
