如何使用XlsxWriter

时间:2018-06-13 21:46:12

标签: python python-3.x xlsxwriter

docs中的以下示例很有用:

import xlsxwriter

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

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

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

# Write some strings with multiple formats.
worksheet.write_rich_string('A1',
                            'This is ',
                            bold, 'bold',
                            ' and this is ',
                            italic, 'italic')

但我怎么做 粗体和斜体

以下不起作用。

worksheet.write_rich_string('A1',
                            'This is ',
                            bold, 'bold',
                            ', this is ',
                            italic, 'italic',                                
                            ', and this is',
                            bold and italic, ' bold and italic')

1 个答案:

答案 0 :(得分:4)

您必须设置另一种粗体和斜体格式。

例如:

bold_and_italic = workbook.add_format({'bold': True, 'italic': True})

完全可重复的版本:

import xlsxwriter

workbook = xlsxwriter.Workbook('rich_strings.xlsx')
worksheet = workbook.add_worksheet()
worksheet.set_column('A:A', 50)

bold = workbook.add_format({'bold': True})
italic = workbook.add_format({'italic': True})
red = workbook.add_format({'color': 'red'})
bold_and_italic = workbook.add_format({'bold': True, 'italic': True})


worksheet.write_rich_string('A1',
                            'This is', bold, ' bold', 
                            ' and this is ', italic, ' italic',  
                            ' and this is', bold_and_italic, ' bold and italic')

workbook.close()

预期输出: Expected Output