Python Xlsxwriter 简明教程
Python XlsxWriter - Number Formats
在 Excel 中,数字数据的不同格式化选项在 Format Cells 菜单的 Number tab 中提供。
In Excel, different formatting options of numeric data are provided in the Number tab of Format Cells menu.

要用 XlsxWriter 控制数字的格式化,我们可以使用 set_num_format() 方法或定义 add_format() 方法的 num_format 属性。
To control the formatting of numbers with XlsxWriter, we can use the set_num_format() method or define num_format property of add_format() method.
f1 = wb.add_format()
f1.set_num_format(FormatCode)
#or
f1 = wb.add_format('num_format': FormatCode)
Excel 有许多预定义的数字格式。它们可以在上方图片所示的数字选项卡的自定义类别中找到。例如,带有两个小数点和逗号分隔符的数字的格式代码是 , #0.00。
Excel has a number of predefined number formats. They can be found under the custom category of Number tab as shown in the above figure. For example, the format code for number with two decimal points and comma separator is ,#0.00.
Example
在以下示例中,数字 1234.52 使用不同的格式代码进行格式化。
In the following example, a number 1234.52 is formatted with different format codes.
import xlsxwriter
wb = xlsxwriter.Workbook('hello.xlsx')
ws = wb.add_worksheet()
ws.set_column('B:B', 30)
num=1234.52
num_formats = (
'0.00',
'#,##0.00',
'0.00E+00',
'##0.0E+0',
'₹#,##0.00',
)
ws.write('A1', 'Formatted Number')
ws.write('B1', 'Format')
row = 1
for fmt in num_formats:
format = wb.add_format({'num_format': fmt})
ws.write_number(row, 0, num, format)
ws.write_string(row, 1, fmt)
row += 1
wb.close()