docx_parser_converter.docx_to_html.converters.table_converter module

class docx_parser_converter.docx_to_html.converters.table_converter.TableConverter[source]

Bases: object

A converter class for converting DOCX tables to HTML.

static convert_cell_properties(properties, tblCellMar) str[source]

Converts cell properties to an HTML style attribute.

Parameters:
  • properties – The cell properties to convert.

  • tblCellMar – The table cell margins to apply.

Returns:

The HTML style attribute representing the cell properties.

Return type:

str

Defaults:
  • Word wrapping is enabled with “word-wrap: break-word;” and “word-break: break-all;”.

  • Overflow handling is enabled with “overflow-wrap: break-word;” and “overflow: hidden;”.

  • Vertical alignment defaults to “top”.

Example

The output style attribute might look like:

'width:250pt; border-top:1pt solid #000000; border-left:1pt solid #000000; border-bottom:1pt solid #000000; border-right:1pt solid #000000; background-color:#FFFFFF; padding:5pt 5pt 5pt 5pt; vertical-align:top;'
static convert_cells(cells: Sequence[TableCell], tblCellMar: MarginProperties | None) list[source]

Converts table cells to HTML.

Parameters:
  • cells – The list of cells to convert.

  • tblCellMar – The table cell margins to apply.

Returns:

The list of HTML td elements representing the cells.

Return type:

list

Example

The output HTML td elements might look like:

<td style="width:250pt; padding:5pt 5pt 5pt 5pt;">Cell 1</td>
<td style="width:250pt; padding:5pt 5pt 5pt 5pt;">Cell 2</td>
static convert_grid(columns: Sequence[float]) str[source]

Converts table grid columns to HTML.

Parameters:

columns – The grid columns widths.

Returns:

The HTML colgroup element representing the grid columns.

Return type:

str

Example

The output HTML colgroup might look like:

<colgroup>
    <col style="width:250pt;">
    <col style="width:250pt;">
</colgroup>
static convert_row(row: TableRow, tblCellMar: MarginProperties | None) Element[source]

Converts a table row to HTML.

Parameters:
  • row – The row to convert.

  • tblCellMar – The table cell margins to apply.

Returns:

The HTML tr element representing the row.

Return type:

etree.Element

Example

The output HTML tr might look like:

<tr style="height:20pt;">
    <td style="width:250pt; padding:5pt 5pt 5pt 5pt;">Cell 1</td>
    <td style="width:250pt; padding:5pt 5pt 5pt 5pt;">Cell 2</td>
</tr>
static convert_row_properties(properties: TableRowProperties | None) str[source]

Converts row properties to an HTML style attribute.

Parameters:

properties – The row properties to convert.

Returns:

The HTML style attribute representing the row properties.

Return type:

str

Example

The output style attribute might look like:

'height:20pt; font-weight:bold;'
static convert_rows(rows: Sequence[TableRow], tblCellMar: MarginProperties | None) Element[source]

Converts table rows to HTML.

Parameters:
  • rows – The list of table rows to convert.

  • tblCellMar – The table cell margins to apply.

Returns:

The HTML tbody element representing the rows.

Return type:

etree.Element

Example

The output HTML tbody might look like:

<tbody>
    <tr style="height:20pt;">
        <td style="width:250pt; padding:5pt 5pt 5pt 5pt;">Cell 1</td>
        <td style="width:250pt; padding:5pt 5pt 5pt 5pt;">Cell 2</td>
    </tr>
</tbody>
static convert_table(table: Table) str[source]

Converts a table to its HTML representation.

Parameters:

table (Table) – The table to convert.

Returns:

The HTML representation of the table.

Return type:

str

Example

Given a table with properties, the output HTML string might look like:

<table style="border-collapse: collapse; width:500pt;">
    <colgroup>
        <col style="width:250pt;">
        <col style="width:250pt;">
    </colgroup>
    <tbody>
        <tr style="height:20pt;">
            <td style="width:250pt; padding:5pt 5pt 5pt 5pt;">Cell 1</td>
            <td style="width:250pt; padding:5pt 5pt 5pt 5pt;">Cell 2</td>
        </tr>
    </tbody>
</table>
static convert_table_properties(properties: TableProperties | None) str[source]

Converts table properties to an HTML style attribute.

Parameters:

properties – The table properties to convert.

Returns:

The HTML style attribute representing the table properties.

Return type:

str

Example

The output style attribute might look like:

'border-collapse: collapse; width:500pt; text-align:center; margin-left:20pt; padding: 5pt 5pt 5pt 5pt;'
static is_cell_empty(cell) bool[source]

Check if a cell is empty by verifying if all runs in all paragraphs have no contents.

Parameters:

cell – The cell to check.

Returns:

True if the cell is empty, False otherwise.

Return type:

bool

Example

if TableConverter.is_cell_empty(cell):
    print("Cell is empty")
else:
    print("Cell is not empty")
static map_border_style(val) str[source]

Maps DOCX border style to CSS border style.

Parameters:

val – The DOCX border style.

Returns:

The CSS border style.

Return type:

str

Example

The output CSS border style might look like:

'solid'
static map_vertical_alignment(val) str[source]

Maps DOCX vertical alignment to CSS vertical alignment.

Parameters:

val – The DOCX vertical alignment.

Returns:

The CSS vertical alignment.

Return type:

str

Example

The output CSS vertical alignment might look like:

'top'