Skip to main content
 首页 » 编程设计

Python xlsxwriter write_rich_string 和高亮

2024年10月25日10itcoder

我使用xlsxwriter 生成excel 报告。感谢 write_rich_string 方法,我想在文本的一部分下划线。

format = { 
    'text': self.formats.result, 
    'difference': self.formats.difference_result 
} 
args = [self.current_line, _column] 
for diff in _diff: 
    args.append(format.get(diff.get('kind'))) 
    args.append(diff.get('text')) 
args.append(self.formats.result) 
self.worksheet.write_rich_string(*args) 

其中这些格式:

self.difference_result = self.workbook.add_format() 
self.difference_result.set_font_color('yellow') 
self.difference_result.set_fg_color('red') 
 
self.result = self.workbook.add_format() 
self.result.set_align('top') 
self.result.set_text_wrap() 
self.result.set_border(1) 
self.result.set_border_color('gray') 

我的问题是文本是黄色的,但没有用红色突出显示。 xlsxwriter 可以吗?

请您参考如下方法:

Is this possible with xlsxwriter?

一般来说,如果格式可以在 Excel 中使用,那么在 XlsxWriter 中也是可以的,因为它支持 Excel 的所有单元格格式设置。

很遗憾,您尝试执行的操作在 Excel 中无法实现。来自write_rich_string() docs :

In Excel only the font properties of the format such as font name, style, size, underline, color and effects are applied to the string fragments in a rich string. Other features such as border, background, text wrap and alignment must be applied to the cell.