Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for HTML rowspan and colspan #8

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified example.docx
Binary file not shown.
82 changes: 76 additions & 6 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,99 @@ <h2>
<table>
<thead>
<tr>
<th rowspan="2">
<span>n</span>
</th>
<th colspan="3">
<span>addition</span>
</th>
<th colspan="3">
<span>multiplication</span>
</th>
</tr>
<tr>
<th>
<span>0</span>
</th>
<th>
<span>1</span>
</th>
<th>
<span>2</span>
</th>
<th>
<span>Heading 1</span>
<span>0</span>
</th>
<th>
<span>Heading 2</span>
<span>1</span>
</th>
<th>
<span>2</span>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<span>A1</span>
<span>0</span>
</td>
<td>
<span>0</span>
</td>
<td>
<span>1</span>
</td>
<td>
<span>A2</span>
<span>2</span>
</td>
<td>
<span>0</span>
</td>
<td colspan="2">
<span>0</span>
</td>
</tr>
<tr>
<td>
<span>B1</span>
<span>1</span>
</td>
<td>
<span>1</span>
</td>
<td>
<span>2</span>
</td>
<td>
<span>3</span>
</td>
<td rowspan="2">
<span>0</span>
</td>
<td>
<span>1</span>
</td>
<td>
<span>2</span>
</td>
</tr>
<tr>
<td>
<span>2</span>
</td>
<td>
<span>2</span>
</td>
<td>
<span>3</span>
</td>
<td>
<span>4</span>
</td>
<td>
<span>2</span>
</td>
<td>
<span>B2</span>
<span>4</span>
</td>
</tr>
</tbody>
Expand Down
Binary file modified example.pdf
Binary file not shown.
18 changes: 13 additions & 5 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import io

from red_tape_kit.doc_ast import Attachment, Document, InlineSequence, Section, Table, UnorderedList
from red_tape_kit.doc_ast import Attachment, Document, InlineSequence, Section, Table, TableCellSpan, UnorderedList


doc = Document(
Expand Down Expand Up @@ -66,10 +66,18 @@
body=[
'This is a table, but may be not that much pretty:',
Table(
headings=['Heading 1', 'Heading 2'],
rows=[
['A1', 'A2'],
['B1', 'B2'],
head=[
[
'n',
'addition', TableCellSpan.COLUMN, TableCellSpan.COLUMN,
'multiplication', TableCellSpan.COLUMN, TableCellSpan.COLUMN,
],
[TableCellSpan.ROW, '0', '1', '2', '0', '1', '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'],
],
),
],
Expand Down
73 changes: 68 additions & 5 deletions red_tape_kit/doc_ast.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
from dataclasses import dataclass
from typing import BinaryIO, Dict, List
from enum import Enum
from typing import BinaryIO, Dict, List, Union


class BlockElement:
Expand Down Expand Up @@ -94,15 +95,77 @@ def normalized(self) -> 'Paragraph':
)


class TableCellSpan(Enum):
ROW = object()
COLUMN = object()

def normalized(self) -> 'TableCellSpan':
return self


TableCell = Union[InlineElement, TableCellSpan]


@dataclass
class ElementaryTable:
rows: List[List[TableCell]]

def normalized(self) -> 'ElementaryTable':
return ElementaryTable(
rows=[
[normalized_inline(cell) for cell in row]
for row in self.rows
],
)

def get_column_span(self, row_index: int, column_index: int) -> int:
"""
Returns how many columns the cell spans.
"""
span = 1
while True:
cell = self.get_cell(row_index, column_index + span)
if cell is TableCellSpan.COLUMN:
span += 1
else:
return span

def get_row_span(self, row_index: int, column_index: int) -> int:
"""
Returns how many rows the cell spans.
"""
span = 1
while True:
cell = self.get_cell(row_index + span, column_index)
if cell is TableCellSpan.ROW:
span += 1
else:
return span

def get_cell(self, row_index: int, column_index: int) -> TableCell:
if row_index < 0 or row_index >= len(self.rows):
return None
row = self.rows[row_index]
if column_index < 0 or column_index >= len(row):
return None
return row[column_index]


@dataclass
class Table(BlockElement):
headings: List[InlineElement]
rows: List[List[InlineElement]]
head: ElementaryTable
body: ElementaryTable

def normalized(self) -> 'Table':
head = self.head
if isinstance(head, list):
head = ElementaryTable(rows=head)
body = self.body
if isinstance(body, list):
body = ElementaryTable(rows=body)
return Table(
headings=[normalized_inline(heading) for heading in self.headings],
rows=[[normalized_inline(cell) for cell in row] for row in self.rows],
head=head.normalized(),
body=body.normalized(),
)


Expand Down
25 changes: 16 additions & 9 deletions red_tape_kit/docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from docx.oxml.ns import qn
from docx.oxml.shared import OxmlElement

from .doc_ast import DefinitionList, Image, Paragraph, Section, Sequence, Table, UnorderedList
from .doc_ast import DefinitionList, Image, Paragraph, Section, Sequence, Table, TableCellSpan, UnorderedList


class DOCXRenderer:
Expand Down Expand Up @@ -115,15 +115,22 @@ def add_paragraph(self, paragraph, list_level, first_in_list_item):
self.docx.add_paragraph(paragraph.text.plain_string, style=style)

def add_table(self, table_data):
table = self.docx.add_table(rows=1, cols=len(table_data.headings))
column_count = len(table_data.head.rows[0])
table = self.docx.add_table(
rows=0,
cols=column_count,
)
table.style = 'Table Grid'
hdr_cells = table.rows[0].cells
for i, heading in enumerate(table_data.headings):
hdr_cells[i].text = heading.plain_string
for data_row in table_data.rows:
row_cells = table.add_row().cells
for i, data_cell in enumerate(data_row):
row_cells[i].text = data_cell.plain_string
self.add_elementary_table(table, table_data.head)
self.add_elementary_table(table, table_data.body)

def add_elementary_table(self, table, elementary_table):
for row in elementary_table.rows:
doc_cells = table.add_row().cells
for i, cell in enumerate(row):
if isinstance(cell, TableCellSpan):
continue
doc_cells[i].text = cell.plain_string

def add_unordered_list(self, unordered_list, list_level):
if list_level is None:
Expand Down
29 changes: 19 additions & 10 deletions red_tape_kit/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from base64 import b64encode

from .doc_ast import (
Attachment, DefinitionList, Image, InlineSequence, Paragraph, Section, Sequence, Table, Text, UnorderedList,
Attachment, DefinitionList, Image, InlineSequence, Paragraph, Section, Sequence, Table, TableCellSpan, Text,
UnorderedList,
)


Expand Down Expand Up @@ -76,16 +77,24 @@ def add_paragraph(self, html_el, paragraph):
def add_table(self, html_el, table_data):
table = ET.SubElement(html_el, 'table')
thead = ET.SubElement(table, 'thead')
tr = ET.SubElement(thead, 'tr')
for heading in table_data.headings:
th = ET.SubElement(tr, 'th')
self.add_inline_element(th, heading)
self.add_elementary_table(thead, table_data.head, cell_tag='th')
tbody = ET.SubElement(table, 'tbody')
for data_row in table_data.rows:
tr = ET.SubElement(tbody, 'tr')
for data_cell in data_row:
td = ET.SubElement(tr, 'td')
self.add_inline_element(td, data_cell)
self.add_elementary_table(tbody, table_data.body, cell_tag='td')

def add_elementary_table(self, html_el, elementary_table, cell_tag):
for ri, row in enumerate(elementary_table.rows):
tr = ET.SubElement(html_el, 'tr')
for ci, cell in enumerate(row):
if isinstance(cell, TableCellSpan):
continue
td = ET.SubElement(tr, cell_tag)
column_span = elementary_table.get_column_span(ri, ci)
if column_span > 1:
td.set('colspan', str(column_span))
row_span = elementary_table.get_row_span(ri, ci)
if row_span > 1:
td.set('rowspan', str(row_span))
self.add_inline_element(td, cell)

def add_unordered_list(self, html_el, unordered_list):
ul = ET.SubElement(html_el, 'ul')
Expand Down
21 changes: 13 additions & 8 deletions red_tape_kit/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from fpdf import FPDF, TitleStyle, XPos, YPos

from .doc_ast import (
Attachment, DefinitionList, Image, InlineSequence, Paragraph, Section, Sequence, Table, Text, UnorderedList,
Attachment, DefinitionList, Image, InlineSequence, Paragraph, Section, Sequence, Table, TableCellSpan, Text,
UnorderedList,
)


Expand Down Expand Up @@ -118,15 +119,19 @@ def add_paragraph(self, paragraph):

def add_table(self, table_data):
with self.table() as table:
heading_row = table.row()
for heading in table_data.headings:
heading_row.cell(heading.plain_string)
for data_row in table_data.rows:
table_row = table.row()
for data_cell in data_row:
table_row.cell(data_cell.plain_string)
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:
pdf_row = pdf_table.row()
for cell in row:
if isinstance(cell, TableCellSpan):
pdf_row.cell('')
else:
pdf_row.cell(cell.plain_string)

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