Julio
Julio

Reputation: 2523

Python xlsxwriter write_rich_string and highlight

I use xlsxwriter to generate an excel report. I would like to underline a part of the text thanks to the write_rich_string method.

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)

where these formats:

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

My problem is that the text is in yellow but not highligh in red. Is this possible with xlsxwriter?

Upvotes: 2

Views: 5025

Answers (2)

Jose Rondon
Jose Rondon

Reputation: 390

This works:

import xlsxwriter
outfile  = xlsxwriter.Workbook('file.xlsx')
sheet    = outfile.add_worksheet()
underlin = outfile.add_format({'underline': True})
sheet.write_rich_string(0,0,"This text as usual, ",underlin,"this underlined, ","and this as usual")
outfile.close()

Upvotes: 1

jmcnamara
jmcnamara

Reputation: 41664

Is this possible with xlsxwriter?

In general if the formatting is possible in Excel it is possible in XlsxWriter since it supports all of Excel's cell formatting.

Unfortunately, what you are trying to do isn't possible in Excel. From the 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.

Upvotes: 2

Related Questions