我正在使用Pandas中的一组数据框。将这些数据框打印到.xlsx文件时,我将每个数据框打印到不同的选项卡。我使用xlsxwriter在保存之前格式化工作表。当我尝试执行除将单元格对齐到“中心”以外的任何其他操作时,我都遇到了麻烦。该脚本没有设置列宽或行高的问题。我无法使文本自动换行或垂直居中。
我尝试使用以下代码,但从未将文本包装在.xlsx文件中已保存或垂直居中的位置。它什么都不做。
cell_format2.set_text_wrap()
cell_format2.set_align('center')
cell_format2.set_align('vcenter')
我还尝试过在add_format函数中进行如下格式化:
cell_format2 = workbook.add_format({'text_wrap': True, 'valign': True})
使用add_format函数始终会出现以下错误:
AttributeError: 'bool' object has no attribute 'lower'
这是我的代码,其中我使用xlsxwriter将3个数据帧打印到3个单独的选项卡上
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter(r'Ashland_Detailed_MTO.xlsx', engine='xlsxwriter')
# Write each dataframe to a different worksheet.
df1.to_excel(writer, index=None, header=True, sheet_name='List A')
df3.to_excel(writer, index=None, header=True, sheet_name='List B')
df5.to_excel(writer, index=None, header=True, sheet_name='List C')
workbook = writer.book
cell_format = workbook.add_format()
cell_format.set_align('center')
cell_format2 = workbook.add_format()
cell_format2.set_text_wrap()
cell_format2.set_align('center')
worksheet = writer.sheets['List A']
worksheet.freeze_panes(1, 0)
worksheet.set_column('A:A', 35, cell_format)
worksheet.set_column('B:B', 12, cell_format)
worksheet.set_column('C:C', 6, cell_format)
worksheet.set_column('D:D', 118, cell_format)
worksheet.set_column('E:H', 7, cell_format)
worksheet.set_column('I:J', 15, cell_format)
worksheet.set_column('K:N', 10, cell_format)
worksheet = writer.sheets['List B']
worksheet.freeze_panes(1, 0)
worksheet.set_column('A:G', 15, cell_format)
worksheet = writer.sheets['List C']
worksheet.set_row(0, 45, cell_format2)
worksheet.freeze_panes(1, 0)
worksheet.set_column('A:G', 12, cell_format)
worksheet.set_column('H:H', 17, cell_format)
worksheet.set_column('I:I', 26, cell_format)
worksheet.set_column('J:J', 20, cell_format)
worksheet.set_column('K:K', 57, cell_format)
worksheet.set_column('L:L', 20, cell_format)
worksheet.set_column('M:M', 57, cell_format)
worksheet.set_column('N:N', 16, cell_format)
worksheet.set_column('O:O', 20, cell_format)
worksheet.set_column('P:AL', 13, cell_format)
worksheet.set_column('AM:AM', 40, cell_format)
writer.save()
每次我打开保存的.xlsx文件时,格式如下所示(第一张图片)。我希望它按照第二张图片所示的格式进行格式化。
答案 0 :(得分:2)
在xlsxwriter
(https://xlsxwriter.readthedocs.io/working_with_pandas.html)的文档站点上,您可以找到以下信息:
Pandas使用默认的单元格格式写入数据帧头。由于它是一种单元格格式,因此无法使用set_row()覆盖。如果您希望对标题使用自己的格式,那么最好的方法是关闭Pandas的自动标题并编写自己的标题。
它们还提供了一个示例,其中在将数据帧写入不带标题的xlsx文件之后,分别写入标题:
# Turn off the default header and skip one row to allow us to insert a
# user defined header.
df.to_excel(writer, sheet_name='Sheet1', startrow=1, header=False)
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Add a header format.
header_format = workbook.add_format({
'bold': True,
'text_wrap': True,
'valign': 'top',
'fg_color': '#D7E4BC',
'border': 1})
# Write the column headers with the defined format.
for col_num, value in enumerate(df.columns.values):
worksheet.write(0, col_num + 1, value, header_format)
在您的情况下,代码将更改为:
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter(r'Ashland_Detailed_MTO.xlsx', engine='xlsxwriter')
# Write each dataframe to a different worksheet.
df1.to_excel(writer, index=None, startrow=1, header=False, sheet_name='List A')
df3.to_excel(writer, index=None, startrow=1, header=False, sheet_name='List B')
df5.to_excel(writer, index=None, startrow=1, header=False, sheet_name='List C')
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
# Add a header format.
header_format = workbook.add_format({
'bold': True,
'text_wrap': True,
'valign': 'top',
'border': 1})
# Write the column headers with the defined format.
worksheet = writer.sheets['List A']
for col_num, value in enumerate(df1.columns.values):
worksheet.write(0, col_num + 1, value, header_format)
worksheet = writer.sheets['List B']
for col_num, value in enumerate(df3.columns.values):
worksheet.write(0, col_num + 1, value, header_format)
...
当数据帧过多时,您可以考虑将格式打包成一个循环并遍历所有工作表。