【问题标题】:How to remove "The number in the cell is formatted as text" using xlsxwrite python如何使用xlsxwriter python删除“单元格中的数字格式为文本”
【发布时间】:2021-09-17 16:51:56
【问题描述】:

我正在编写 python 脚本,我将一些数据保存在 excel 工作表中。为此,我使用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表格:

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

【问题讨论】:

  • 你在这里写文字"{}%".format(chsl_spread)
  • 为什么D列有不同的值类型?
  • @Sabil 你应该能够复制它。 “为什么 D 列有不同的值类型”我只是为了测试而这样做,没有什么逻辑。

标签: python excel xlsxwriter


【解决方案1】:

要在 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()

输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    相关资源
    最近更新 更多