0

我正在尝试使用 Python 在 Excel 文件中添加美元符号 ($)。我已将数字格式编码为'"$"#,##0.00'(参见下面的代码)。

我运行代码并下载 Excel 文件。但是当我打开 Excel 文件时,它会显示英镑符号。

请注意:我的默认会计格式符号是“£ English UK”。我需要将货币符号显示为$使用 Python。如何?

    wb = Workbook()
    ws0 = wb.add_sheet('Sheet 1')

    currency_style = XFStyle()        
    currency_style.num_format_str =  '"$"#,##0.00'

    for i in range(1,5):           
        ws0.write(i, 0, 125.05, currency_style) 


    self.response.headers['Content-Type'] = 'application/ms-excel'
    self.response.headers['Content-Transfer-Encoding'] = 'Binary'
    self.response.headers['Content-disposition'] = 'attachment; filename="myfile.xls"'
    wb.save(self.response.out)
4

2 回答 2

1

这段代码工作正常。问题在于您的默认会计格式符号。如果它设置为英镑符号,您的工作表将在您打开文件时将美元符号替换为英镑。您需要更改此设置以显示美元符号。

于 2011-11-02T08:26:34.830 回答
0

您需要弄清楚 Excel 如何处理多个货币符号,然后您可以担心如何使用 python 中的相同解决方案。可能就像在您的格式中转义 $ 一样简单,谁知道呢?这可能会为您指明正确的方向:

http://blogs.office.com/b/microsoft-excel/archive/2005/12/02/quick-detour-2-working-with-currencies-made-easy.aspx

亚历克西斯

于 2011-11-04T15:41:37.173 回答