Excel for Mac is quite inconvenient because you cannot specify multiple tables at once when creating a pivot table. (It should have been done in the old version ...) First of all, we will make it easier to handle the data by combining the data of multiple sheets into one sheet with python.
Use openpyxl. It is a library for performing operations such as reading and writing Excel. https://openpyxl.readthedocs.io/en/stable/
Install openpyxl
pip install openpyxl
#Read file
read_path = "./input.xlsx"
output_path = "./output.xlsx"
wb = load_workbook(read_path)
#Get data for all sheets
sheet_names = wb.get_sheet_names()
first_row = 1
l_2d_output = []
for sheet_name in sheet_names:
sheet = wb.get_sheet_by_name(sheet_name)
for row in sheet.iter_rows(min_row=first_row):
l = []
for cell in row:
l.append(str(cell.value))
l_2d_output.append(l)
#Skip reading headers from next sheet
first_row = 2
#Write to file
def write_list_2d(sheet, l_2d, start_row, start_col):
for y, row in enumerate(l_2d):
for x, cell in enumerate(row):
sheet.cell(row=start_row + y,
column=start_col + x,
value=l_2d[y][x])
#writing
output_wb = openpyxl.Workbook()
output_ws = output_wb.active
write_list_2d(output_ws, l_2d_output, 1, 1)
output_wb.save(output_path)
The code has too much room for improvement, I was able to use it as much as I used, so I'm happy with it.
Recommended Posts