Skip to content

Commit

Permalink
Merge pull request #22 from AutoActuary/dev-column-gutter-widths
Browse files Browse the repository at this point in the history
Feature: Add optional argument `col_margin_width` to set gutter column widths.
  • Loading branch information
rudolfbyker authored Nov 1, 2024
2 parents 514abcf + 94070ba commit 3c701e6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 13 deletions.
13 changes: 13 additions & 0 deletions aa_py_openpyxl_util/_write_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ def write_tables_side_by_side_over_multiple_sheets(
tables: Sequence[TableInfo],
row_margin: int,
col_margin: int,
col_margin_width: int | None = None,
write_captions: bool,
write_pre_rows: bool,
max_sheet_width: int,
Expand All @@ -242,6 +243,7 @@ def write_tables_side_by_side_over_multiple_sheets(
tables: A sequence of table info objects.
row_margin: The number of empty rows to leave above each table.
col_margin: The number of empty columns to leave to the left of each table.
col_margin_width: The width of the margin columns. If None, the column width is left at the default.
write_captions: Whether to write the table name and description above the table. This shifts the table down.
write_pre_rows: Whether to write the pre_rows (below the name and description, but above the table header).
max_sheet_width:
Expand Down Expand Up @@ -273,6 +275,7 @@ def write_tables_side_by_side_over_multiple_sheets(
tables=tables_in_sheet,
row_margin=row_margin,
col_margin=col_margin,
col_margin_width=col_margin_width,
write_captions=write_captions,
write_pre_rows=write_pre_rows,
)
Expand All @@ -287,6 +290,7 @@ def write_tables_side_by_side(
tables: Sequence[TableInfo],
row_margin: int,
col_margin: int,
col_margin_width: int | None = None,
write_captions: bool,
write_pre_rows: bool,
) -> WrittenTablesInSheet:
Expand All @@ -305,6 +309,7 @@ def write_tables_side_by_side(
tables: A sequence of table info objects.
row_margin: The number of empty rows to leave above each table.
col_margin: The number of empty columns to leave to the left of each table.
col_margin_width: The width of the margin columns. If None, the column width is left at the default.
write_captions: Whether to write the table name and description above the table. This shifts the table down.
write_pre_rows: Whether to write the pre_rows (below the name and description, but above the table header).
Expand Down Expand Up @@ -369,6 +374,14 @@ def write_tables_side_by_side(

first_column += col_margin + width

if col_margin and col_margin_width:
# Set the width of the gutter columns.
for name, ((r, c), table) in results.items():
if c > 1:
sheet.column_dimensions[get_column_letter(c - 1)].width = (
col_margin_width
)

return results


Expand Down
71 changes: 58 additions & 13 deletions test/write_only/test_write_tables_side_by_side.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Callable

from openpyxl import Workbook
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.table import TableStyleInfo

from aa_py_openpyxl_util import (
Expand Down Expand Up @@ -59,7 +60,7 @@ def test(book: Workbook) -> None:
get_cell_values(table2_sheet[table2_range]),
)

test_helper(write, test)
test_helper(write, test, True)

def test_same_length(self) -> None:
def write(book: Workbook) -> None:
Expand Down Expand Up @@ -107,7 +108,7 @@ def test(book: Workbook) -> None:
get_cell_values(table2_sheet[table2_range]),
)

test_helper(write, test)
test_helper(write, test, True)

def test_different_lengths(self) -> None:
def write(book: Workbook) -> None:
Expand Down Expand Up @@ -168,7 +169,7 @@ def test(book: Workbook) -> None:
get_cell_values(table3_sheet[table3_range]),
)

test_helper(write, test)
test_helper(write, test, True)

def test_captions(self) -> None:
def write(book: Workbook) -> None:
Expand Down Expand Up @@ -203,7 +204,7 @@ def test(book: Workbook) -> None:
self.assertEqual("Table1", table1_sheet["B2"].value)
self.assertEqual("First table", table1_sheet["B3"].value)

test_helper(write, test)
test_helper(write, test, True)

def test_pre_rows(self) -> None:
def write(book: Workbook) -> None:
Expand Down Expand Up @@ -245,7 +246,7 @@ def test(book: Workbook) -> None:
self.assertEqual("Pre B", table1_sheet["C2"].value)
self.assertEqual("Pre C", table1_sheet["D2"].value)

test_helper(write, test)
test_helper(write, test, True)

def test_pre_rows_wider_than_table(self) -> None:
"""
Expand Down Expand Up @@ -295,7 +296,7 @@ def test(book: Workbook) -> None:
table2_sheet, table2_range = find_table(book=book, name="Table2")
self.assertEqual("E3:E6", table2_range)

test_helper(write, test)
test_helper(write, test, True)

def test_pre_rows_narrower_than_table(self) -> None:
"""
Expand Down Expand Up @@ -345,7 +346,7 @@ def test(book: Workbook) -> None:
table2_sheet, table2_range = find_table(book=book, name="Table2")
self.assertEqual("E3:F6", table2_range)

test_helper(write, test)
test_helper(write, test, True)

def test_values(self) -> None:
def write(book: Workbook) -> None:
Expand Down Expand Up @@ -388,7 +389,7 @@ def test(book: Workbook) -> None:
get_cell_values(table_sheet[table_range]),
)

test_helper(write, test)
test_helper(write, test, True)

def test_formulas(self) -> None:
def write(book: Workbook) -> None:
Expand Down Expand Up @@ -487,7 +488,7 @@ def test(book: Workbook) -> None:
get_cell_values(table_sheet[table_range]),
)

test_helper(write, test)
test_helper(write, test, True)

def test_number_format(self) -> None:
def write(book: Workbook) -> None:
Expand Down Expand Up @@ -531,7 +532,50 @@ def test(book: Workbook) -> None:
get_cell_values(table_sheet[table_range]),
)

test_helper(write, test)
test_helper(write, test, True)

def test_column_margins(self) -> None:
def write(book: Workbook) -> None:
write_tables_side_by_side(
book=book,
sheet_name="Sheet1",
tables=[
TableInfo(
name="Table1",
column_names=["a", "b"],
rows=[[FormattedCell(1), FormattedCell(2)]],
),
TableInfo(
name="Table2",
column_names=["c", "d", "e"],
rows=[[FormattedCell(3), FormattedCell(4), FormattedCell(5)]],
),
],
row_margin=1,
col_margin=1,
col_margin_width=3,
write_captions=False,
write_pre_rows=False,
)

def test(book: Workbook) -> None:
sheet = book["Sheet1"]
column_letters = [get_column_letter(i + 1) for i in range(7)]
self.assertEqual(
{
"A": 3.0,
"B": 13.0,
"C": 13.0,
"D": 3.0,
"E": 13.0,
"F": 13.0,
"G": 13.0,
},
{col: sheet.column_dimensions[col].width for col in column_letters},
)

# Setting column widths has no effect in `write_only` mode. This is a limitation of `openpyxl`.
test_helper(write, test, False)


class TestWriteTablesSideBySideOverMultipleSheets(unittest.TestCase):
Expand Down Expand Up @@ -572,7 +616,7 @@ def test(book: Workbook) -> None:
)
self.assertEqual("Tables1", table2_sheet.title)

test_helper(write, test)
test_helper(write, test, True)

def test_one_sheet(self) -> None:
table1 = TableInfo(
Expand Down Expand Up @@ -611,14 +655,15 @@ def test(book: Workbook) -> None:
)
self.assertEqual("Tables", table2_sheet.title)

test_helper(write, test)
test_helper(write, test, True)


def test_helper(
write: Callable[[Workbook], None],
test: Callable[[Workbook], None],
write_only: bool,
) -> None:
book = Workbook(write_only=True)
book = Workbook(write_only=write_only)
write(book)

with TemporaryDirectory() as tmp_dir:
Expand Down

0 comments on commit 3c701e6

Please sign in to comment.