How to 'format cells' with openpyxl?

耗尽温柔 提交于 2021-01-28 06:28:29

问题


I want to format the column cells in my excel sheet using openpyxl to have their numbers decimal places at '0'.

Example Sheet:

     B           C
63245634566     NAME
63562341234     NAME
23452345345     NAME
21345234554     NAME
41234123442     NAME
23542345345     NAME
6.24333E+11     NAME
43242334233     NAME

Output '6.24333E+11'

Wanted Output '62433323422'


回答1:


Try

from openpyxl import load_workbook
wb = load_workbook( 'so_12387212.xlsx' )
ws = wb[ wb.sheetnames[0] ]
cell11 = ws.cell(1, 1)
cell11.number_format = '0'
wb.save( 'so_12387212.xlsx' )
wb.close()

Adapt it as needed.




回答2:


Select all the rows in column B and change it to Numeric. 1- select all the numbers - go to the home tab - change from general to numeric. 2- select all of the numbers - right-click the mouse - select change format - change it to numeric and change it to number.



来源:https://stackoverflow.com/questions/57318721/how-to-format-cells-with-openpyxl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!