documentation for setting table/cell border is available here: http://officeopenxml.com/WPtableBorders.php
i've reused the existing code available to set table cell border answered here https://stackoverflow.com/a/49615968/5736491 and modified it to set single table property. this will set the uniform property for all borders within the table, and it is stored at the table level (instead of at the individual cell level):
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.table import Table
from docx import Document
def set_table_border(table: Table, **kwargs):
"""
Sets table border
Usage:
set_table_border(
table,
top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},
bottom={"sz": 12, "color": "#00FF00", "val": "single"},
start={"sz": 24, "val": "dashed", "shadow": "true"},
end={"sz": 12, "val": "dashed"},
)
"""
tbl = table._tbl
tblPr = tbl.tblPr
# check for tag existnace, if none found, then create one
tblBorders = tblPr.first_child_found_in("w:tblBorders")
if tblBorders is None:
tblBorders = OxmlElement('w:tblBorders')
tblPr.append(tblBorders)
# list over all available tags
for edge in ('start', 'top', 'end', 'bottom', 'insideH', 'insideV'):
edge_data = kwargs.get(edge)
if edge_data:
tag = 'w:{}'.format(edge)
# check for tag existnace, if none found, then create one
element = tblBorders.find(qn(tag))
if element is None:
element = OxmlElement(tag)
tblBorders.append(element)
# looks like order of attributes is important
for key in ["sz", "val", "color", "space", "shadow"]:
if key in edge_data:
element.set(qn('w:{}'.format(key)), str(edge_data[key]))
if __name__ == "__main__":
doc = Document()
t = doc.add_table(rows=1, cols=3)
border_prop = {
'sz': '16', # table border thickness (8=1pt => 16 = 2pt)
'val': 'single', # line style
'color': 'auto' # border color
}
set_table_border(t, top=border_prop, bottom=border_prop,
end=border_prop, start=border_prop,
insideH=border_prop, insideV=border_prop)
doc.save('borders.docx')