Changeset 5bcc3ba in rattail


Ignore:
Timestamp:
10/30/2022 04:56:02 PM (3 months ago)
Author:
Lance Edgar <lance@…>
Branches:
master
Children:
a2e54d9
Parents:
0189de2
Message:

Make some behavior optional, for writing Excel reports

Location:
rattail
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • rattail/excel.py

    r0189de2 r5bcc3ba  
    196196    """
    197197
    198     def __init__(self, path, fields, sheet_title=None, number_formats={}):
     198    def __init__(self, path, fields, sheet_title=None, number_formats={},
     199                 highlight_rows=True):
    199200        """
    200201        Constructor; opens an Excel workbook for writing.
     
    207208            self.sheet.title = sheet_title
    208209        self.number_formats = number_formats
     210        self.highlight_rows = highlight_rows
     211
     212    def load_workbook(self, path, clear_sheet=False):
     213        self.book = openpyxl.load_workbook(filename=path)
     214        self.sheet = self.book.active
     215
     216        if clear_sheet:
     217            # remove all non-header rows from the sheet
     218            # TODO: make header row count configurable
     219            self.sheet.delete_rows(2, self.sheet.max_row)
    209220
    210221    def create_sheet(self, title):
     
    251262
    252263        # apply row highlighting
    253         if row % 2 == 0:
    254             fill_even = PatternFill(patternType='solid',
    255                                     fgColor='d9d9d9',
    256                                     bgColor='d9d9d9')
    257             for col, field in enumerate(self.fields, 1):
    258                 cell = self.sheet.cell(row=row, column=col)
    259                 cell.fill = fill_even
     264        if self.highlight_rows:
     265            if row % 2 == 0:
     266                fill_even = PatternFill(patternType='solid',
     267                                        fgColor='d9d9d9',
     268                                        bgColor='d9d9d9')
     269                for col, field in enumerate(self.fields, 1):
     270                    cell = self.sheet.cell(row=row, column=col)
     271                    cell.fill = fill_even
    260272
    261273    def write_rows(self, rows, progress=None):
  • rattail/reporting/excel.py

    r0189de2 r5bcc3ba  
    5252       Optional list of fields which should be "totalled" and represented in a
    5353       final totals row within the output.
     54
     55    .. attr:: write_data_header
     56
     57       Boolean flag (true by default) indicating that a header row
     58       with field names should be written to the data sheet.  Set this
     59       to false if you are using a template file.
     60
     61    .. attr:: auto_format_data
     62
     63       Boolean flag (true by default) indicating that certain
     64       "auto-formatting" should be applied to the data sheet.
     65
     66    .. attr:: include_summary_sheet
     67
     68       Boolean flag (true by default) indicating that a second
     69       "summary" sheet should be added to the output file.
    5470    """
    5571    output_fields = []
    5672    number_formats = {}
    5773    totalled_fields = []
     74    write_data_header = True
     75    auto_format_data = True
     76    include_summary_sheet = True
    5877
    5978    def make_filename(self, session, params, data, **kwargs):
     
    8099        Write the primary data sheet for the Excel output file.
    81100        """
    82         writer.write_header()
     101        if self.write_data_header:
     102            writer.write_header()
    83103        fields = self.get_output_fields(params)
    84104
     
    117137                cell.fill = fill_totals
    118138
    119         writer.auto_freeze()
    120         writer.auto_filter()
    121         writer.auto_resize()
     139        if self.auto_format_data:
     140            writer.auto_freeze()
     141            writer.auto_filter()
     142            writer.auto_resize()
    122143
    123144    def write_summary_sheet(self, writer, session, params, data,
     
    160181        self.write_data_sheet(writer, session, params, data, progress=progress)
    161182
    162         self.write_summary_sheet(writer, session, params, data, progress=progress)
     183        if self.include_summary_sheet:
     184            self.write_summary_sheet(writer, session, params, data,
     185                                     progress=progress)
    163186
    164187        writer.save(progress=progress)
Note: See TracChangeset for help on using the changeset viewer.