【发布时间】:2021-03-01 12:03:48
【问题描述】:
我有一个 xlsx 文件,我正在尝试写入,但由于某种原因,我似乎无法对要在单元格中换行的文本进行格式化:
我已经尝试过如下方式(已经删除了我认为不重要的行),但是文本不会换行,如果我执行其他条件格式设置它可以工作,那么此文本不会换行:
the df is defined...
writer = pd.ExcelWriter(f"{xlsx_file}", engine='xlsxwriter')
df['Statistics'].to_excel(writer, sheet_name='Statistics', index=False)
...
text_wrap = workbook.add_format({'text_wrap': True})
...
for sheet in all_sheets:
worksheet = writer.sheets[f"{sheet}"]
worksheet.set_column(1, 6, 25)
if sheet == "Statistics":
worksheet.conditional_format(f'A1:A16', {'type': 'cell',
'criteria': '!=',
'value': '0',
'format': text_wrap})
writer.save()
为了简单起见,我将工作簿和其他我认为不相关的行从上面的代码中删除。 这个想法是有一个包含多个工作表的工作簿,并且希望仅将条件格式应用于特定工作表中的一列(第一列)。
感谢您的帮助
【问题讨论】:
标签: python-3.x pandas conditional-formatting xlsxwriter