[英]Applying formatting to multiple Excel sheets using Python and XLSXWriter
我有两个数据框如下:
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()
给这个 output
或者,我可以使用以下代码将两个数据框导出为单独的工作表而不进行格式化:
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()
如何手动或递归地将格式应用于所有工作表?
XlsxWriter 库中没有简单的方法可以做到这一点,自 2014 年以来这一直是一个问题。( https://github.com/jmcnamara/XlsxWriter/issues/111 )您可以使用 go 并使用工作表。 ) 方法(就像您已经做过的那样),或者与助手 function 一起工作。 我刚刚找到了这个库:
https://github.com/webermarcolivier/xlsxpandasformatter
xlsxpandasformatter 库为 XlsxWriter 和 pd.to_excel() 提供了几个帮助程序 function。
您可能会 go 并执行以下操作:
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()
此 [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://stackoverflow.com/a/57350467/2781105
我使用以下 function 使用 Xlsxwriter 格式化 Excel 中的两个选项卡。 我有两个数据框,这完全符合我的预期。
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)
然后我像这样写了 output 并调用了 function:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.