简体   繁体   English

通过python中的xlwings调整Excel中的数字格式

[英]Adjust number formatting in Excel via xlwings from python

I have started using xlwings to create an excel tool which calls a python code.我已经开始使用 xlwings 创建一个调用 python 代码的 excel 工具。 I think it is super useful and at the same time user-friendly as everybody is used to have excel as GUI.我认为它非常有用,同时用户友好,因为每个人都习惯将 excel 作为 GUI。

Now to my problem: The tool works well.现在我的问题:该工具运行良好。 However, I am left with some formatting.但是,我留下了一些格式。 Currently I am able to do some formatting (range(XX).number_format = XX ), but I have not managed to create my desired format.目前我可以进行一些格式化(range(XX).number_format = XX ),但我还没有设法创建我想要的格式。

I want to have comma separated numbers without decimals.我想要没有小数的逗号分隔数字。

sht = xw.Book.caller().sheets[0]

sht.range('C:D').number_format = '0.00' (1)

sht.range('C:D').number_format = 'General' (2)

sht.range('C:D').number_format = '#’##0' (3)

(1): This works. (1):这有效。 However, numbers are not comma separated (as expected)但是,数字不是逗号分隔的(如预期的那样)

(2): Does not work. (2):不起作用。 Python runs and runs, nothing happens. Python运行并运行,没有任何反应。 (same for 'Number') (与“数字”相同)

(3): Produces desired results on my machine/ my excel version. (3):在我的机器/我的 excel 版本上产生所需的结果。 However, on my friend's excel it looks different and does not produce the desired results anymore.但是,在我朋友的 excel 上,它看起来有所不同,并且不再产生预期的结果。

Thanks a lot in advance for your help.非常感谢您的帮助。

In order to display the format in thousands and seperate by comma, I tried this,为了以千位显示格式并用逗号分隔,我尝试了这个,

number_format = "#,##0"

and it works perfectly in my case.它在我的情况下完美运行。

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

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