diff --git a/conservancy_beancount/reports/accrual.py b/conservancy_beancount/reports/accrual.py index f37cb2999c31aa4ea2043e087d4c47aa0abca96a..3d8535b4f835ab605831360f7f41a19c673e4378 100644 --- a/conservancy_beancount/reports/accrual.py +++ b/conservancy_beancount/reports/accrual.py @@ -299,18 +299,6 @@ class AgingODS(core.BaseODS[AccrualPostings, Optional[data.Account]]): self.date = date self.logger = logger - def init_styles(self) -> None: - super().init_styles() - self.style_widecol = self.replace_child( - self.document.automaticstyles, - odf.style.Style, - name='WideCol', - ) - self.style_widecol.setAttribute('family', 'table-column') - self.style_widecol.addElement(odf.style.TableColumnProperties( - columnwidth='1.25in', - )) - def section_key(self, row: AccrualPostings) -> Optional[data.Account]: if isinstance(row.account, str): return row.account @@ -321,7 +309,7 @@ class AgingODS(core.BaseODS[AccrualPostings, Optional[data.Account]]): for accrual_type in AccrualAccount: self.use_sheet(accrual_type.name.title()) for index in range(self.COL_COUNT): - stylename = self.style_widecol if index else '' + stylename = self.style_col1_25 if index else '' self.sheet.addElement(odf.table.TableColumn(stylename=stylename)) self.add_row(*( self.string_cell(name, stylename=self.style_bold) diff --git a/conservancy_beancount/reports/core.py b/conservancy_beancount/reports/core.py index 551003b7b5ed85a847280defe6742a20d15eee13..77f5e64b3ac8b56ff730fe3ba9543a17211502bd 100644 --- a/conservancy_beancount/reports/core.py +++ b/conservancy_beancount/reports/core.py @@ -837,16 +837,15 @@ class BaseODS(BaseSpreadsheet[RT, ST], metaclass=abc.ABCMeta): self.ensure_child( self.style_bold, odf.style.TextProperties, fontweight='bold', ) - self.style_starttext: odf.style.Style - self.style_centertext: odf.style.Style - self.style_endtext: odf.style.Style - for textalign in ['start', 'center', 'end']: - aligned_style = self.replace_child( - styles, odf.style.Style, name=f'{textalign.title()}Text', - ) - aligned_style.setAttribute('family', 'table-cell') - aligned_style.addElement(odf.style.ParagraphProperties(textalign=textalign)) - setattr(self, f'style_{textalign}text', aligned_style) + self.style_dividerline = self.ensure_child( + styles, odf.style.Style, name='DividerLine', family='table-cell', + ) + self.ensure_child( + self.style_dividerline, + odf.style.TableCellProperties, + borderbottom='1pt solid #0000ff', + ) + date_style = self.replace_child(styles, odf.number.DateStyle, name='ISODate') date_style.addElement(odf.number.Year(style='long')) date_style.addElement(odf.number.Text(text='-')) @@ -860,14 +859,31 @@ class BaseODS(BaseSpreadsheet[RT, ST], metaclass=abc.ABCMeta): family='table-cell', datastylename=date_style, ) - self.style_dividerline = self.ensure_child( - styles, odf.style.Style, name='DividerLine', family='table-cell', - ) - self.ensure_child( - self.style_dividerline, - odf.style.TableCellProperties, - borderbottom='1pt solid #0000ff', - ) + + self.style_starttext: odf.style.Style + self.style_centertext: odf.style.Style + self.style_endtext: odf.style.Style + for textalign in ['start', 'center', 'end']: + aligned_style = self.replace_child( + styles, odf.style.Style, name=f'{textalign.title()}Text', + ) + aligned_style.setAttribute('family', 'table-cell') + aligned_style.addElement(odf.style.ParagraphProperties(textalign=textalign)) + setattr(self, f'style_{textalign}text', aligned_style) + + self.style_col1: odf.style.Style + self.style_col1_25: odf.style.Style + self.style_col1_5: odf.style.Style + self.style_col1_75: odf.style.Style + self.style_col2: odf.style.Style + for width in ['1', '1.25', '1.5', '1.75', '2']: + width_name = width.replace('.', '_') + column_style = self.replace_child( + self.document.automaticstyles, odf.style.Style, name=f'col_{width_name}', + ) + column_style.setAttribute('family', 'table-column') + column_style.addElement(odf.style.TableColumnProperties(columnwidth=f'{width}in')) + setattr(self, f'style_col{width_name}', column_style) ### Rows and cells diff --git a/tests/test_reports_spreadsheet.py b/tests/test_reports_spreadsheet.py index 8333c71a48fbcd96b3da5f1b0561b84c5825c2a2..97780bc69a417f97c528f0607a700e2e34799033 100644 --- a/tests/test_reports_spreadsheet.py +++ b/tests/test_reports_spreadsheet.py @@ -261,6 +261,11 @@ def test_ods_currency_style_cache_considers_properties(ods_writer): assert plain.getAttribute('datastylename') != bold.getAttribute('datastylename') @pytest.mark.parametrize('attr_name,child_type,checked_attr', [ + ('style_col1', odf.style.TableColumnProperties, 'columnwidth'), + ('style_col1_25', odf.style.TableColumnProperties, 'columnwidth'), + ('style_col1_5', odf.style.TableColumnProperties, 'columnwidth'), + ('style_col1_75', odf.style.TableColumnProperties, 'columnwidth'), + ('style_col2', odf.style.TableColumnProperties, 'columnwidth'), ('style_bold', odf.style.TextProperties, 'fontweight'), ('style_centertext', odf.style.ParagraphProperties, 'textalign'), ('style_dividerline', odf.style.TableCellProperties, 'borderbottom'), @@ -268,12 +273,12 @@ def test_ods_currency_style_cache_considers_properties(ods_writer): ('style_starttext', odf.style.ParagraphProperties, 'textalign'), ]) def test_ods_writer_style(ods_writer, attr_name, child_type, checked_attr): + if child_type is odf.style.TableColumnProperties: + root = ods_writer.document.automaticstyles + else: + root = ods_writer.document.styles style = getattr(ods_writer, attr_name) - actual = get_child( - ods_writer.document.styles, - odf.style.Style, - name=style.getAttribute('name'), - ) + actual = get_child(root, odf.style.Style, name=style.getAttribute('name')) assert actual is style child = get_child(actual, child_type) assert child.getAttribute(checked_attr)