简体   繁体   中英

xlsxwriter: conditional formatting on entire column

I would like to apply conditional formatting to an entire column. It doesn't seem to like my range notation though this notation seems allowable from looking at other posts.

I've tried explicitly naming the range and I still get errors.

Code:

import xlsxwriter

xlsx_file = REPORTS + "\\" + project + report_date + '_inventory.xlsx'
writer = pd.ExcelWriter(xlsx_file, engine='xlsxwriter')

df.to_excel(writer, sheet_name='Not Supported', startrow = 1, header=False,index=False)
wb = writer.book
ws = writer.sheets['Not Supported']
(max_row, max_col) = df.shape

column_settings = [{'header': column} for column in df.columns]
ws.add_table(0,0,max_row,max_col-1,{'columns': column_settings,
                                     'style': 'Table Style Medium 8'})
ws.set_column(0, max_col-1,15)
ws.conditional_format('G1:G1048576', {'type': 'cell',
                               'criteria': '=',
                               'value': 'False',
                               'format': 'Bad'})

writer.save()
writer.close()

Error:

 AttributeError                            Traceback (most recent call last)
<ipython-input-46-099ede3fda94> in <module>
     47                                      'style': 'Table Style Medium 8'})
     48 ws4.set_column(0, max_col-1,15)
---> 49 ws4.conditional_format('G1:G1048576', {'type': 'cell',
     50                                        'criteria': 'equal to',
     51                                        'value': '"False"',

~\anaconda3\lib\site-packages\xlsxwriter\worksheet.py in cell_wrapper(self, *args, **kwargs)
     98             args = new_args
     99 
--> 100         return method(self, *args, **kwargs)
    101 
    102     return cell_wrapper

~\anaconda3\lib\site-packages\xlsxwriter\worksheet.py in conditional_format(self, first_row, first_col, last_row, last_col, options)
   2280         # Get the dxf format index.
   2281         if 'format' in options and options['format']:
-> 2282             options['format'] = options['format']._get_dxf_index()
   2283 
   2284         # Set the priority based on the order of adding.

AttributeError: 'str' object has no attribute '_get_dxf_index'

To apply a conditional format to an entire column with XlsxWriter you need to give the entire range of the column like this:

ws.conditional_format('G1:G1048576', {'type': 'cell',
                                      'criteria': '=',
                                      'value': 'False',
                                      'format': 'Bad'})

This is explained in the Row and Column Ranges section of the docs. The 'G:G' shortcut syntax isn't supported by XlsxWriter but the full range 'G1:G1048576' will be displayed as 'G:G' in Excel.

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