2
votes

I have an excel file that contains multiple sheets (sheet1, sheet2, sheet3, sheet4, etc). I would like to make some formatting changes using python and save those edited files as a separate excel file with multiple sheets.

Step 1: Read the file containing multiple sheets

Step 2: Make changes in the sheets

Step 3: Save the changes to an excel file containing multiple sheets

What I tried so far?

import pandas as pd
import pandas.io.formats.style
import os
from pandas import ExcelWriter
import numpy as np
import xlsxwriter
from itertools import cycle

from xlsxwriter.utility import xl_rowcol_to_cell

df = pd.ExcelFile('data.xlsx')

for i in df.sheet_names:
    # make the required changes
    writer = pd.ExcelWriter('edited_file.xlsx', engine='xlsxwriter', options={'strings_to_numbers': True}, date_format='mmmm dd yyyy')  
    df.to_excel(writer, sheet_name= i , startrow=1 , startcol=0, header=False, index=False, encoding='utf8')  
    workbook  = writer.book
    worksheet = writer.sheets[i]

    data_format1 = workbook.add_format({'bg_color': '#F1EEEE'})
    data_format2 = workbook.add_format({'bg_color': '#FFFFFF'})

    formats = cycle([data_format1, data_format2])

    for row, value in enumerate(df):
        data_format = next(formats)
        worksheet.set_row(row, cell_format=data_format)
        worksheet.write(row, 0, value)

    format_header = workbook.add_format()
    format_header.set_bold()
    format_header.set_border()
    format_header.set_bg_color('gray')
    format_header.set_font_color('white')

    format_data = workbook.add_format()

    #worksheet.set_column('A:Z', 20, format_data)
    worksheet.set_row(0, 20, format_header)

    # Write the header manually
    for colx, value in enumerate(df.columns.values):
        worksheet.write(0, colx, value)

    worksheet.autofilter(0, 0, df.shape[0], df.shape[1])

    writer.save()

I am unable to save multiple sheets, only the first sheet is saved. How do I do that?

2

2 Answers

1
votes

You have to move pd.ExcelWriter outside the loop and call writer.save() only once, as soon as the loop ends:

writer = pd.ExcelWriter('edited_file.xlsx', engine='xlsxwriter', options={'strings_to_numbers': True}, date_format='mmmm dd yyyy')
for i in df.sheet_names:
    ...
    ...
    ...
    worksheet.autofilter(0, 0, df.shape[0], df.shape[1])
writer.save()

Moreover, I think you should use ExcelFile in a different way (at least if you use openpyxl):

exc = pd.ExcelFile('data.xlsx', engine='openpyxl')

writer = pd.ExcelWriter('edited_file.xlsx', engine='xlsxwriter', options={'strings_to_numbers': True}, date_format='mmmm dd yyyy')
for i in exc.sheet_names:
    df = exc.parse(i)
    ...
    ...
2
votes

When you call this in the for loop you close the file at the very first iteration:

 writer.save()

You've to call it outside your for loop