Setting Cell Formats with xlwt format strings

心不动则不痛 提交于 2020-01-23 04:31:05

问题


I've looked around for several different xlwt formats for cells, but I can't find a comprehensive list. Excel provides the following options for cell content formatting: (general, number, currency, accounting, date, time, percentage, fraction, scientific, text, special, custom) Obviously custom isn't what I want, but what are the formatting strings for all these different options? For example, I've seen:

date_format = XFStyle()
date_format.num_format_str = 'dd/mm/yyyy'

currency_format = XFStyle()
currency_format.num_format_str = '$#,##0.00'

Is there somewhere I can get a comprehensive list of those formatting strings? The documentation on http://www.python-excel.org/ doesn't have much, and the tutorial at the bottom only seems to have dates.


回答1:


I've found a list. I was just being stupid about looking for it.
The GitHub project has a list of number format strings here:
https://github.com/python-excel/xlwt/blob/master/examples/num_formats.py




回答2:


import time
import datetime
Time = datetime.datetime.now()
Currency = 234

#Format Date
style1 = xlwt.XFStyle()
style1.num_format_str = 'DD-MM-YY'

#Format Currency
style2 = xlwt.XFStyle()
style2.num_format_str = '$#,##0.00'

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet1')

worksheet.write(0, 1, Time, style1)
worksheet.write(0, 2, Currency, style2)


来源:https://stackoverflow.com/questions/17887160/setting-cell-formats-with-xlwt-format-strings

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