简体   繁体   English

使用 python 将数字格式添加到 xlxs 文件

[英]To add number format to xlxs file using python

I have data like below in abc.xlxs我在 abc.xlxs 中有如下数据

date,qty,price,profitprice,sellprice
20200501,11,900,,20

And using python I want output as:并使用 python 我想要 output 为:

data,qty,price,profitprice,sellprice
20200501,11.00,900.00,,20.00

Can any one help on this?有人可以帮忙吗?

how can I read each column with its value and add number format and save to xlxs file?如何读取每列及其值并添加数字格式并保存到 xlxs 文件?

Based on this answer by Akshit Khurana :根据Akshit Khurana回答

import pandas as pd

df = pd.read_excel("initial.xlsx")

writer = pd.ExcelWriter("formatted.xlsx", engine = "xlsxwriter")
df.to_excel(writer, index=False, header=True)
workbook  = writer.book
worksheet = writer.sheets['Sheet1']
format1 = workbook.add_format({'num_format': '0.00'})
worksheet.set_column('C:E', None, format1)  # Adds formatting to columns C-E
writer.save()

I believe the two other answers posted here do not work for the same reason why this question was asked.我相信这里发布的其他两个答案不起作用,原因与提出这个问题的原因相同。

You can use the dtype parameter at read_excel :您可以在read_excel使用 dtype 参数:

pd.read_excel('abc.xlxs', dtype={'profitprice': float, 'sellprice': float})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM