在 XLS 或 CSV 文件中用不同颜色写入单元格的一部分

Write a part of a cell with different color in a XLS or CSV file

有没有办法在 CSV 或 XLS 文件中用颜色写入单元格的一部分?我正在使用 Python 并且为了展示我的结果,我想使用颜色来突出显示单元格的一部分。单元格包含文本。如果 XLS 无法做到这一点,最好的选择是什么?

如果你可以使用 xlsx 而不是 xls/csv 那么你可以使用 XlsxWriter 的 write_rich_string() 方法:

import xlsxwriter

workbook = xlsxwriter.Workbook('rich_strings.xlsx')
worksheet = workbook.add_worksheet()

worksheet.set_column('A:A', 30)

# Set up some formats to use.
red = workbook.add_format({'color': 'red'})
blue = workbook.add_format({'color': 'blue'})

worksheet.write_rich_string('A1',
                            'This is ',
                            red, 'red',
                            ' and this is ',
                            blue, 'blue')

workbook.close()

输出:

请参阅 write_rich_string()

上的 XlsxWriter 文档