python - How to apply multiple formats to one column with XlsxWriter -
in below code apply number formatting each of columns in excel sheet. however, can't seem figure out apply multiple formattings specific column, either centering or numbering end being on written. possible apply 2 types of formatting 1 column?
def to_excel(video_report): # create pandas excel writer using xlsxwriter engine. writer = pd.excelwriter('pandas_simple.xlsx', engine='xlsxwriter') # convert dataframe xlsxwriter excel object. video_report.to_excel(writer, sheet_name='sheet1', na_rep="-") # xlsxwriter objects dataframe writer object. workbook = writer.book worksheet = writer.sheets['sheet1'] # add cell formats. integer = workbook.add_format({'num_format': '0'}) decimal = workbook.add_format({'num_format': '0.00'}) percentage = workbook.add_format({'num_format': '0.0%'}) center = workbook.add_format({'align': 'center'}) # apply centering worksheet.set_column('b:l', none, center) # apply number formatting worksheet.set_column('b:b', 13, integer ) worksheet.set_column('c:c', 17, percentage ) worksheet.set_column('d:d', 19, percentage ) worksheet.set_column('e:e', 15, integer ) worksheet.set_column('f:f', 15, percentage ) worksheet.set_column('g:g', 15, decimal ) worksheet.set_column('h:h', 13, integer ) worksheet.set_column('i:i', 13, integer ) worksheet.set_column('j:j', 13, integer ) worksheet.set_column('k:k', 13, integer ) worksheet.set_column('l:l', 13, integer ) writer.save() to_excel(video_report)
is possible apply 2 types of formatting 1 column?
yes. adding 2 or more properties format object using. example:
integer = workbook.add_format({'num_format': '0', 'align': 'center'})
see xlsxwriter docs on how use formats.
Comments
Post a Comment