• 主页
  • 换行文本xlsxwriter列pd dataframe python

换行文本xlsxwriter列pd dataframe python

我有一个试图写入的xlsx文件,但由于某种原因,似乎无法对要在单元格中换行的文本进行格式化:

我尝试过像bellow (删除了我认为不重要的行),但是如果我使用其他条件,文本将不会换行

_

格式化它工作,这篇文章包装它不是你:

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()

为了简单起见,我从上面的代码中去掉了工作簿和其他我认为不相关的行。其想法是,有一个包含多个工作表的工作簿,并且希望将条件格式仅应用于特定工作表中的一列(第一列)。

谢谢你的帮助

转载请注明出处:http://www.jubohx.com/article/20230511/2506724.html