I am going through the tutorial and documentation of python-docx.
However, I can't find any reference to how I can specify and manipulate the border appearance of a table created in a Microsoft Word document.
When i use the following code:
from docx import Document
from docx.shared import Inches
document = Document()
################################
################################
################################
table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
row_cells = table.add_row().cells
row_cells[0].text = 'Str0'
row_cells[1].text = 'Str1'
row_cells[2].text = 'Str2'
row2_cells = table.add_row().cells
row2_cells[0].text = 'Str00'
row2_cells[1].text = 'Str11'
row2_cells[2].text = 'Str22'
################################
################################
################################
document.save('demo.docx')
The resulting docx
file shows the 3x3
table with header. Currently there is no black border (inner or outer) on this table.
How can I specify an upper and lower border around the header, a lower border to wrap around the table, as well as an inner vertical border?
For example:
答案 0 :(得分:4)
不幸的是,没有办法直接操纵python-docx
内的表格边框。您基本上需要使用style来指定这些。该样式必须 已经 存在于Word中。
为了根据自己的喜好生成表格样式,您需要在Word中手动创建一个新的空文档,自定义其中一个现有的表格样式(例如Colorful Shading
),并使用此修改后的表格添加样式。接下来,删除此表并将文件(例如example.docx
)保存在磁盘上。这是将修改的样式保存在文档中所必需的。
然后你需要做的就是使用python-docx
加载文件并添加一个新表,它将引用(修改过的)表样式:
document = Document('example.docx')
table = document.add_table(rows=1, cols=3)
table.style = 'ColorfulShading'
有关Word中样式如何工作的详细信息,另请参阅文档中的Understanding styles。
答案 1 :(得分:1)
由于python-docx库目前没有直接设置单元格边框的函数方法,我从这个web(强烈推荐的讲座)中找到了这个解决方案:
from docx.table import _Cell
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
def set_cell_border(cell: _Cell, **kwargs):
"""
Set cell`s border
Usage:
set_cell_border(
cell,
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"},
)
"""
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
# check for tag existnace, if none found, then create one
tcBorders = tcPr.first_child_found_in("w:tcBorders")
if tcBorders is None:
tcBorders = OxmlElement('w:tcBorders')
tcPr.append(tcBorders)
# 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 = tcBorders.find(qn(tag))
if element is None:
element = OxmlElement(tag)
tcBorders.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]))
列出了用于编辑单元格边框的可选关键字参数列表 here。