Skip to content

Commit

Permalink
Colspan and rowspan support in PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
SupraSummus committed Jan 8, 2024
1 parent 2287a4a commit 2d51797
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 36 deletions.
Binary file modified example.docx
Binary file not shown.
31 changes: 11 additions & 20 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ <h2>
<thead>
<tr>
<th rowspan="2">
<span>n</span>
<span>number</span>
</th>
<th colspan="3">
<span>addition</span>
Expand All @@ -104,22 +104,22 @@ <h2>
</tr>
<tr>
<th>
<span>0</span>
<span>of 0</span>
</th>
<th>
<span>1</span>
<span>of 1</span>
</th>
<th>
<span>2</span>
<span>of 2</span>
</th>
<th>
<span>0</span>
<span>by 0</span>
</th>
<th>
<span>1</span>
<span>by 1</span>
</th>
<th>
<span>2</span>
<span>by 2</span>
</th>
</tr>
</thead>
Expand All @@ -141,24 +141,21 @@ <h2>
<span>0</span>
</td>
<td colspan="2">
<span>0</span>
<span>0 (colspan)</span>
</td>
</tr>
<tr>
<td>
<span>1</span>
</td>
<td>
<span>1</span>
</td>
<td>
<span>2</span>
<td colspan="2" rowspan="2">
<span>who cares? (rowspan &amp; colspan)</span>
</td>
<td>
<span>3</span>
</td>
<td rowspan="2">
<span>0</span>
<span>0 (rowspan)</span>
</td>
<td>
<span>1</span>
Expand All @@ -171,12 +168,6 @@ <h2>
<td>
<span>2</span>
</td>
<td>
<span>2</span>
</td>
<td>
<span>3</span>
</td>
<td>
<span>4</span>
</td>
Expand Down
Binary file modified example.pdf
Binary file not shown.
14 changes: 9 additions & 5 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,20 @@
Table(
head=[
[
'n',
'number',
'addition', TableCellSpan.COLUMN, TableCellSpan.COLUMN,
'multiplication', TableCellSpan.COLUMN, TableCellSpan.COLUMN,
],
[TableCellSpan.ROW, '0', '1', '2', '0', '1', '2'],
[
TableCellSpan.ROW,
'of 0', 'of 1', 'of 2',
'by 0', 'by 1', 'by 2',
],
],
body=[
['0', '0', '1', '2', '0', '0', TableCellSpan.COLUMN],
['1', '1', '2', '3', '0', '1', '2'],
['2', '2', '3', '4', TableCellSpan.ROW, '2', '4'],
['0', '0', '1', '2', '0', '0 (colspan)', TableCellSpan.COLUMN],
['1', 'who cares? (rowspan & colspan)', TableCellSpan.COLUMN, '3', '0 (rowspan)', '1', '2'],
['2', TableCellSpan.ROW, TableCellSpan.ROW, '4', TableCellSpan.ROW, '2', '4'],
],
),
],
Expand Down
13 changes: 8 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ packages = [

[tool.poetry.dependencies]
python = "^3.11"
fpdf2 = {version = "^2.7.6", optional = true}
# TODO: update to official release once PR https://github.com/py-pdf/fpdf2/pull/1088 is released
fpdf2 = {url = "https://github.com/py-pdf/fpdf2/archive/f099a32bdc5f21e468183a83ff700d5dd7570c1f.zip", optional = true}
python-docx = {version = "^1.1.0", optional = true}
beautifulsoup4 = {version = "^4.12.2", optional = true}

Expand Down
16 changes: 11 additions & 5 deletions red_tape_kit/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,25 @@ def add_paragraph(self, paragraph):
return self.add_inline_element(paragraph.text)

def add_table(self, table_data):
with self.table() as table:
with self.table(
num_heading_rows=len(table_data.head.rows),
) as table:
self.add_elementary_table(table, table_data.head)
self.add_elementary_table(table, table_data.body)
return True

def add_elementary_table(self, pdf_table, elementary_table):
for row in elementary_table.rows:
for ri, row in enumerate(elementary_table.rows):
pdf_row = pdf_table.row()
for cell in row:
for ci, cell in enumerate(row):
if isinstance(cell, TableCellSpan):
pdf_row.cell('')
pass
else:
pdf_row.cell(cell.plain_string)
pdf_row.cell(
cell.plain_string,
colspan=elementary_table.get_column_span(ri, ci),
rowspan=elementary_table.get_row_span(ri, ci),
)

def add_unordered_list(self, unordered_list):
orig_left_margin = self.l_margin
Expand Down

0 comments on commit 2d51797

Please sign in to comment.