首次创建工作簿时,如何使用xlswriter永久设置字体大小?
我试过了:
book = xlsxwriter.Workbook(os.getcwd() + '\\test.xlsx')
sheet1 = book.add_worksheet()
format = book.add_format()
format.set_font_size(10)
但我仍然在输出中获得默认大小11。有什么问题?
答案 0 :(得分:5)
1,对于单格字体大小,您必须将format
传递给write
,如下所示:
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()
# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})
# Add a font size 10 format.
format = workbook.add_format()
format.set_font_size(10)
# Write some simple text.
worksheet.write('A1', 'Hello', format)
# Text with formatting.
worksheet.write('A2', 'World', bold)
# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)
workbook.close()
Hello
将设置为字体大小10
。
<强>更新强> 2,对于所有单元格字体大小,您可以设置工作簿的默认格式:
import xlsxwriter
workbook = xlsxwriter.Workbook('demo.xlsx')
# default cell format to size 10
workbook.formats[0].set_font_size(10)
worksheet = workbook.add_worksheet()
# Write some simple text.
worksheet.write('A1', 'Hello')
worksheet.write('A2', 'World')
# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)
workbook.close()
所有单元格都将更改为字体大小10: