Changeset - 973e47ae51b7
[Not reviewed]
0 1 0
Lance Edgar (lance) - 4 years ago 2020-09-18 11:15:35
lance@edbob.org
Allow overriding column header labels when writing Excel file
1 file changed with 5 insertions and 2 deletions:
0 comments (0 inline, 0 general)
rattail/excel.py
Show inline comments
 
@@ -108,28 +108,31 @@ class ExcelWriter(object):
 
    def __init__(self, path, fields, sheet_title=None, number_formats={}):
 
        """
 
        Constructor; opens an Excel workbook for writing.
 
        """
 
        self.path = path
 
        self.fields = fields
 
        self.book = openpyxl.Workbook()
 
        self.sheet = self.book.active
 
        if sheet_title:
 
            self.sheet.title = sheet_title
 
        self.number_formats = number_formats
 

	
 
    def write_header(self):
 
    def write_header(self, labels=None):
 
        font = Font(bold=True)
 
        for i, field in enumerate(self.fields, 1):
 
            cell = self.sheet.cell(row=1, column=i, value=field)
 
            value = field
 
            if labels and field in labels:
 
                value = labels[field]
 
            cell = self.sheet.cell(row=1, column=i, value=value)
 
            cell.font = font
 

	
 
    def write_row(self, data, row=None):
 
        """
 
        Write (append) a single data row to the current sheet.
 

	
 
        :param row: The 1-based row number to which data should be written.
 
        """
 
        if row is None:
 
            raise NotImplementedError("should be able to detect 'next' row here?")
 

	
 
        self.sheet.append(data)
0 comments (0 inline, 0 general)