如何使用 xlsxwrite python 删除 "The number in the cell is formatted as text"

How to remove "The number in the cell is formatted as text" using xlsxwrite python

我正在编写 python 脚本,我将在 excel sheet 中保存一些数据。为此,我使用 xlsxwriter。我有以下代码:

out_xls_path = os.path.join(dir_path, 'moprime', 'output.xlsx')
workbook = xlsxwriter.Workbook(out_xls_path)
io_sheet = workbook.add_worksheet("Input-Output ()")

io_sheet.set_column(1, 3, 25)
style = workbook.add_format({'bold': True, 'font_name': 'Calibri', 'align': 'center', 'border': 1})
io_sheet.write('C1', "ch/sl spread", style)
io_sheet.write('D1', "{}%".format(chsl_spread), style)
io_sheet.write('C2', "ch/sl spread", style)
io_sheet.write('D2', 23, style)

做上面的,下面就是excel sheet:

我想在 D1 中了解如何删除 The number in the cell is formatted as text 中的那个小点。谢谢

要在 Excel 中创建百分比数字,您需要除以 100 并应用数字格式,例如“0.00%”。如果将其格式化为字符串,您将收到您看到的警告。

这是一个基于您的示例的工作示例:


import xlsxwriter

out_xls_path = 'output.xlsx'
workbook = xlsxwriter.Workbook(out_xls_path)
io_sheet = workbook.add_worksheet("Input-Output ()")

io_sheet.set_column(1, 3, 25)

style = workbook.add_format({'bold': True,
                             'font_name': 'Calibri',
                             'align':
                             'center', 'border': 1})

percent = workbook.add_format({'bold': True,
                               'font_name': 'Calibri',
                               'align':
                               'center', 'border': 1,
                               'num_format': '0.00%'})

chsl_spread = 29.82

io_sheet.write('C1', "ch/sl spread", style)
io_sheet.write('D1', chsl_spread / 100.0, percent)
io_sheet.write('C2', "ch/sl spread", style)
io_sheet.write('D2', 23, style)

workbook.close()

输出: