简体   繁体   中英

I want to display my EXCEL data with “%” sign. How can i do so by using python

i am creating an Excel data sheet where i need to show my data both in number and with a percentage sign. but when i use percentage from Excel data type it adds some extra zero (9600%). but i don't want it like this. my requirement for data is only with the percentage sign (96%). Can i do this by using Excel of Python.

Using a 0.42 as value for your cell will lead to 42% - setting 42 into that cell, you will get 4200% .

from openpyxl import Workbook  # openpyxel 2.5.9

wb = Workbook()
ws = wb[wb.sheetnames[0]]
_cell = ws.cell(1,1)
_cell.number_format = '%'
_cell.value = 42                   # 4200 %

_cell = ws.cell(2,1)
_cell.number_format = '%'
_cell.value = 0.42                 # 42 %

wb.save("sample.xlsx")

格式化


In case you use earlier versions of openpyxel or other packages to create your excel the syntax might differ, the solution should be the same. Set a below-one float as cell value to get percentages less then 100%.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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