如何将字符串值的数据帧作为格式化数值写入 excel 文件?

How to write a dataframe of string values to an excel file as formated numeric values?

基本上我有一个字符串值列表,这些值将被转换为数字并打印为 excel sheet。 我已经可以做到了 但我希望这些值打印为“excel 数值。这是我得到的:

writer = pd.ExcelWriter('test.xlsx', engine='openpyxl')

listOfValues = ['3913375,45','126179,15','22324,72','26335,28','3699901,34']

df_values = pd.DataFrame({'TOTAL': listOfValues}).apply(pd.to_numeric)

df_values.to_excel(writer, sheet_name='test')

writer.save()

我的结果是:

Resultant Excel

但我希望将其打印为由点分隔的数值,以便更容易准备,就像这样:

What i want to have as result

(我想要的解决方案不能在结果 excel 中打印为字符串,它需要是数值,就像我们使用选项“格式为数字(Shift+ctrl+1)”一样在 libreoffice calc 中)。

(我使用 Python 3.8.5 和 LibreOffice Calc 进行测试,但结果旨在 Excel 2016 年使用)。

这可能吗?感谢您的帮助,抱歉我的英语不好!

勾选这个https://xlsxwriter.readthedocs.io/working_with_pandas.html

It is possible to format any other, non date/datetime column data using set_column()

尝试从这里更改格式化示例代码:

format1 = workbook.add_format({'num_format': '#,##0.00'})

对此:

format1 = workbook.add_format({'num_format': '#.##0,00'})

我找到了,虽然不一样,但我想答案是一样的

from openpyxl import load_workbook

wb = load_workbook('a.xlsx')
ws = wb[wb.sheetnames[0]]
cell11 = ws.cell(1, 1)
cell11.number_format = '#,##0.00'
wb.save('b.xlsx')
wb.close()

它适用于该格式,但它似乎不适用于 '#.##0,00' 格式