【问题标题】:wrap text xlsxwriter columns pd dataframe python换行文本 xlsxwriter 列 pd 数据框 python
【发布时间】: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


    【解决方案1】:

    我已经尝试如下(...),但文本不会换行,如果我做其他条件格式它可以工作,这个文本不会换行

    Excel 不支持条件格式的文本换行或其他对齐属性,因此 XlsxWriter 格式只会被忽略(由 Excel)。

    【讨论】:

    • 嗯,没有其他方法可以让它写成人类可读的吗?这个想法是一些单元格有 60 个字符。而其他人则很少,不知道如何使其更具可读性。我正在考虑将列宽做得更大,但这样一切都会向右走。
    猜你喜欢
    • 2021-10-09
    • 2019-08-01
    • 2021-09-06
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 2021-12-28
    相关资源
    最近更新 更多