I have an .xlsx file with multiple sheets and I want to split all the sheets into different .xlsx files. For example, I have a file, matt.xlsx and it has 5 sheets with names A,B,C,D,E. I want to split them into 5 files naming them a.xlsx, b.xlsx, c.xlsx, d.xlsx and e.xlsx.
The code I have written is as below but it does not write anything to the split files and have an error when I try to open them saying "Format of the file does not match"
import os
import csv
from openpyxl import load_workbook
import pandas as pd
def main():
# read wbook from xlsx,
filepath = os.path.join(os.getcwd())
wb = load_workbook(filename='matt.xlsx')
dest_dir = os.path.join('desktop','new files')
os.makedirs(dest_dir, exist_ok=True)
# loop the sheet
for sheet in wb.worksheets:
sheet_names = sheet.title.split(" ")
sheet_names = [x.lower() for x in sheet_names]
dest_path = os.path.join(dest_dir, sheet_names.xlsx')
with open(dest_path, 'w') as f:
writer = pd.ExcelWriter(f, engine='xlsxwriter')
reader = sheet.rows
next(reader, None)
Thank you in advance. I am a beginner using python and I would be grateful if anyone could help.