使用 xlsxwriter 写入单元格时是否可以应用多种单元格格式?

Is it possible to apply multiple cell formats when writing to a cell with xlsxwriter?

我是 xlsxwriter 的新手,非常感谢您的帮助。我正在尝试将多种单元格格式应用于 xlsxwriter 中的一个单元格,环顾四周后我没有看到任何信息表明这是可能的,但想在这里询问并确定。

我的代码片段:

wb = xlsxwriter.Workbook('test.xlsx')
ws1 = wb.add_worksheet()
calign = wb.add_format()
calign.set_align('center')
wrap = wb.add_format()
wrap.set_text_wrap()

ws1.write(0, 0, 'Username', calign, wrap)

它给我错误:

TypeError: write_string() takes at most 5 arguments (6 given)

我理解错误,因为我给它的参数多了 1 个,超出了它的承受能力。但是xlsxwriter中有类似xlwt中easyXF的功能吗?

用于编写应用多种单元格格式的单元格的 easyxf 函数示例:

format = xlwt.easyxf('alignment: horiz centre, wrap on')
ws1.write(0,0,'Text', format)

我正在使用 xlsxwriter 的条件格式功能,所以切换到 xlwt 不是一个选项,除非单元格格式变得比条件格式更重要。

谢谢。

在研究了 source and documentation 之后,我不得不说恐怕 不可能 指定多个单元格格式,或将多个格式合并为一个 Format class.

必须添加新格式:

new_format = wb.add_format()
new_format.set_align('center')
new_format.set_text_wrap()

ws1.write(0, 0, 'Username', new_format)

仅供参考,还有一个 write_rich_string() 方法,它允许多种格式 - 但我认为这不是您要找的,因为它会将多种格式应用于不同的 parts/fragments一个字符串。