| name | python-docx-3-table-creation-and-formatting |
| description | Sub-skill of python-docx: 3. Table Creation and Formatting. |
| version | 1.0.0 |
| category | data |
| type | reference |
| scripts_exempt | true |
3. Table Creation and Formatting
3. Table Creation and Formatting
"""
Create and format tables with merged cells, styles, and custom formatting.
"""
from docx import Document
from docx.shared import Inches, Pt, Cm, Twips
from docx.enum.table import WD_TABLE_ALIGNMENT, WD_ROW_HEIGHT_RULE
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import nsdecls, qn
docx.oxml parse_xml, OxmlElement
typing , , ,
() -> :
shading_elm = OxmlElement()
shading_elm.(qn(), color)
cell._tc.get_or_add_tcPr().append(shading_elm)
() -> :
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
tcBorders = OxmlElement()
border_name [, , , ]:
border = OxmlElement()
border.(qn(), )
border.(qn(), (border_size))
border.(qn(), border_color)
tcBorders.append(border)
tcPr.append(tcBorders)
() -> :
table = doc.add_table(rows=, cols=(headers))
table.alignment = WD_TABLE_ALIGNMENT.CENTER
header_cells = table.rows[].cells
i, header (headers):
cell = header_cells[i]
cell.text = header
set_cell_shading(cell, header_color)
paragraph = cell.paragraphs[]
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = paragraph.runs[]
run.font.bold =
run.font.color.rgb =
run.font.size = Pt()
row_idx, row_data (data):
row = table.add_row()
col_idx, value (row_data):
cell = row.cells[col_idx]
cell.text = (value)
alternate_row_color row_idx % == :
set_cell_shading(cell, alternate_row_color)
(value, (, )):
cell.paragraphs[].alignment = WD_ALIGN_PARAGRAPH.RIGHT
table
() -> :
table = doc.add_table(rows=, cols=)
table.style =
top_left = table.cell(, )
top_right = table.cell(, )
top_left.merge(top_right)
top_left.text =
top_left.paragraphs[].alignment = WD_ALIGN_PARAGRAPH.CENTER
top_left.paragraphs[].runs[].bold =
set_cell_shading(top_left, )
q1_q2 = table.cell(, )
q1_q2_end = table.cell(, )
q1_q2.merge(q1_q2_end)
q1_q2.text =
q1_q2.paragraphs[].alignment = WD_ALIGN_PARAGRAPH.CENTER
set_cell_shading(q1_q2, )
table.cell(, ).text =
set_cell_shading(table.cell(, ), )
table.cell(, ).text =
set_cell_shading(table.cell(, ), )
sub_headers = [, , , ]
i, header (sub_headers):
cell = table.cell(, i)
cell.text = header
set_cell_shading(cell, )
data = [
[, , , ],
[, , , ]
]
row_idx, row_data (data, start=):
col_idx, value (row_data):
table.cell(row_idx, col_idx).text = value
() -> :
doc = Document()
doc.add_heading(, level=)
doc.add_heading(, level=)
headers = [, , , , , ]
data = [
[, , , , , ],
[, , , , , ],
[, , , , , ],
[, , , , , ],
]
create_data_table(doc, headers, data)
doc.add_paragraph()
doc.add_heading(, level=)
create_merged_header_table(doc)
doc.add_paragraph()
doc.add_heading(, level=)
table = doc.add_table(rows=, cols=)
table.style =
widths = [Inches(), Inches(), Inches()]
row table.rows:
idx, (cell, width) ((row.cells, widths)):
cell.width = width
headers = [, , ]
i, header (headers):
cell = table.rows[].cells[i]
cell.text = header
cell.paragraphs[].runs[].bold =
set_cell_shading(cell, )
data = [
[, , ],
[, , ],
[, , ],
*Content truncated — see parent skill full reference.*