使用 Python 和 XLSXWriter 将格式应用于多个 Excel 工作表

Posted

技术标签:

【中文标题】使用 Python 和 XLSXWriter 将格式应用于多个 Excel 工作表【英文标题】:Applying formatting to multiple Excel sheets using Python and XLSXWriter 【发布时间】:2021-05-25 06:46:48 【问题描述】:

我有两个数据框如下:

import pandas as pd 
import numpy as np
from datetime import date

df = pd.DataFrame('Data': [10, 22, 31, 43, 57, 99, 65, 74, 88],
                  'Data2':[10, 22, 31, 43, 57, 99, 65, 74, 88],
                  'Data3':[10, 22, 31, 43, 57, 99, 65, 74, 88])

df2 = pd.DataFrame('df2_Data': ['blue', 'yellow', 'purple', 'orange', 'green', 'brown', 'gray', 'white', 'red'],
                  'df2_Data2':['bike', 'car', 'bus', 'train', 'boat', 'truck', 'plane', 'scooter', 'skateboard'],
                  'df2_Data3':['chicken', 'cow', 'dog', 'crocodile', 'snake', 'pig', 'rat', 'mouse', 'monkey'])

我可以使用以下代码将 df 以所需格式导出为 Excel 中的单个工作表:

today = date.today()
d2 = today.strftime("%B %d, %Y")



writer = pd.ExcelWriter('ExcelExample.xlsx'.format(d2), engine='xlsxwriter')

df.to_excel(writer, sheet_name='Sheet1')
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

header_format = workbook.add_format(
    'bold': True,
    'text_wrap': True,
    'valign': 'top',
    'fg_color': '#38C4F1',
    'font_color': 'FFFFFF',
    'border': 1)

for col_num, value in enumerate(df.columns.values):
    worksheet.write(0, col_num + 1, value, header_format)
    
writer.save()

给出这个输出

或者,我可以使用以下代码将两个数据框导出为单独的工作表而不进行格式化:

writer = pd.ExcelWriter('ExcelExample.xlsx'.format(d2), engine='xlsxwriter')

# Write each dataframe to a different worksheet.
df.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name='Sheet2')

# Close the Pandas Excel writer and output the Excel file.
writer.save()

如何手动或递归地将格式应用于所有工作表?

【问题讨论】:

【参考方案1】:

XlsxWriter 库中没有简单的方法可以做到这一点,自 2014 年以来这一直是一个问题。(https://github.com/jmcnamara/XlsxWriter/issues/111) 您可以使用 worksheet.write() 方法(就像您已经做过的那样),也可以使用辅助函数。我刚刚找到了这个库:

https://github.com/webermarcolivier/xlsxpandasformatter

xlsxpandasformatter 库为 XlsxWriter 和 pd.to_excel() 提供了几个帮助函数。 你可能会去做这样的事情:

from xlsxpandasformatter import FormatedWorksheet
pd.formats.format.header_style = None

with pd.ExcelWriter("output_file.xlsx", engine="xlsxwriter", datetime_format="%B %d, %Y") as writer:
    workbook = writer.book

    header_format = workbook.add_format(
        'bold': True,
        'text_wrap': True,
        'text_v_align': 'top',
        'fg_color': '#38C4F1',
        'font_color': 'FFFFFF',
        'border': 1)
    
    all_df = [df1, df2]
    sheet_num = 1

    for df in all_df:
        sheetname = 'Sheet' + str(sheet_num)
        sheet_num += 1
        df.to_excel(writer, sheet_name = sheetname , index=False)

        worksheet = writer.sheets[sheetname]

        formattedWorksheet = FormatedWorksheet(worksheet, workbook, df)

        formattedWorksheet.format_header(headerFormat=header_format)
        formattedWorksheet.MoreMethodsThatYouCanApply()

        formattedWorksheet.apply_format_table()

【讨论】:

【参考方案2】:

此 [post][1] 中的以下代码使我能够实现将格式应用于多个工作表的目标:

writer = pd.ExcelWriter('ExcelExample.xlsx'.format(d2), engine='xlsxwriter')
sheets_in_writer=['Sheet1','sheet2']

data_frame_for_writer=[df, df2]

for i,j in zip(data_frame_for_writer,sheets_in_writer):
    i.to_excel(writer,j,index=False)
    
#(max_row, max_col) = df.shape
#column_settings = ['header': column for column in df.columns]


### Assign WorkBook
workbook=writer.book
# Add a header format
header_format = workbook.add_format('bold': True,'text_wrap': True,'size':10,
                                                      'valign': 'top','fg_color': '#c7e7ff','border': 1)


### Apply same format on each sheet being saved
for i,j in zip(data_frame_for_writer,sheets_in_writer):
    for col_num, value in enumerate(i.columns.values):
        writer.sheets[j].set_column(0, max_col - 1, 12)
#       writer.sheets[j].add_table(0, 0, max_row, max_col - 1, 'columns': column_settings,'autofilter': True)
        writer.sheets[j].write(0, col_num, value, header_format)
        writer.sheets[j].autofilter(0,0,0,i.shape[1]-1)
        writer.sheets[j].freeze_panes(1,0)
writer.save()


  [1]: https://***.com/a/57350467/2781105

【讨论】:

【参考方案3】:

我使用以下函数在 Excel 中使用 Xlsxwriter 格式化两个选项卡。我有两个数据框,这完全符合我的预期。

def format_excel(writer):
""" Add Excel specific formatting to the workbooks for main report
"""
    workbook = writer.book
    worksheet = writer.sheets['Networks Selected']
    worksheet.set_zoom(120)
    worksheet.autofilter('$A$1:$G$1')
    worksheet2 = writer.sheets['No Primary Selected']
    worksheet2.set_zoom(120)
    worksheet2.autofilter('$A$1:$G$1')

    # Add some cell formats for columns with numbers
    format1 = workbook.add_format('num_format': '00.00%')
    format2 = workbook.add_format('num_format': '0.00')
    format3 = workbook.add_format('num_format': 'mm/dd/yyyy')

    # Set the column width and format. First Sheet
    worksheet.set_column('A:A', 15)
    worksheet.set_column('B:B', 25)
    worksheet.set_column('C:E', 30)
    worksheet.set_column('F:G', 15)
    worksheet.set_column('H:H', 15, format3)

    # Set the column width and format. Second Sheet
    worksheet2.set_column('A:A', 15)
    worksheet2.set_column('B:B', 25)
    worksheet2.set_column('C:G', 30)
    worksheet2.set_column('F:G', 15)
    worksheet2.set_column('H:H', 15, format3)

然后我这样写输出并调用函数:

writer = pd.ExcelWriter(f"Networks Selected List v2.xlsx", engine='xlsxwriter')
df_primary.to_excel(writer, sheet_name='Networks Selected', index = False)
df_no_primary.to_excel(writer, sheet_name='No Primary Selected', index = False)
format_excel(writer)
writer.save()

【讨论】:

以上是关于使用 Python 和 XLSXWriter 将格式应用于多个 Excel 工作表的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 xlsxwriter 和 python 将图像添加到 xlsx 文件的标题?

使用 Python 和 XLSXWriter 将格式应用于多个 Excel 工作表

如何使用 xlsxwriter - python 更改图例字体大小

python使用xlsxwriter

Python——xlsxwriter模块的使用

在Linux环境中使用XlsxWriter