使用 Python 的 docx 库,如何缩进 table?

Using Python's docx library, how can a table be indented?

如何缩进 docx table?我正在尝试将 table 与设置在 2cm 的制表位对齐。以下脚本创建一个 header、一些文本和一个 table:

import docx
from docx.shared import Cm

doc = docx.Document()

style = doc.styles['Normal']
style.paragraph_format.tab_stops.add_tab_stop(Cm(2))

doc.add_paragraph('My header', style='Heading 1')
doc.add_paragraph('\tText is tabbed')

# This indents the paragraph inside, not the table
# style = doc.styles['Table Grid']
# style.paragraph_format.left_indent = Cm(2)

table = doc.add_table(rows=0, cols=2, style="Table Grid")

for rowy in range(1, 5):
    row_cells = table.add_row().cells

    row_cells[0].text = 'Row {}'.format(rowy)
    row_cells[0].width = Cm(5)

    row_cells[1].text = ''
    row_cells[1].width = Cm(1.2)

doc.save('output.docx')

它会产生一个没有 ident 的 table,如下所示:

table如何缩进如下?
(最好不必加载现有文档):

例如,如果将 left-indent 添加到 Table Grid 样式(通过取消注释行),它将应用于段落级别,而不是 table 级别,从而导致以下(不需要):

在 Microsoft Word 中,这可以通过为 Indent from left 输入 2.0 cm 在 table 属性上完成。

python-docx 尚不支持此功能。看起来这种行为是由 w:tbl 元素的 w:tblInd 子元素产生的。您可以开发一个变通函数来添加像这样的元素,使用 w:tbl 元素上的 lxml 调用,该元素应该在 Table 对象的 ._element 属性上可用.

您可以通过搜索 'python-docx workaround function' 找到其他变通方法的示例,通过搜索 'python-pptx workaround functions' 可以找到类似的示例。

我是这样做的:

import docx
import lxml
mydoc = docx.Document()            
mytab = self.mydoc.add_table(3,3)
nsmap=mytab._element[0].nsmap # For namespaces
searchtag='{%s}tblPr' % nsmap['w'] # w:tblPr
mytag='{%s}tblInd' % nsmap['w'] # w:tblInd
myw='{%s}w' % nsmap['w'] # w:w
mytype='{%s}type' % nsmap['w'] # w:type
for elt in mytab._element:
    if elt.tag == searchtag:
        myelt=lxml.etree.Element(mytag)
        myelt.set(myw,'1000')
        myelt.set(mytype,'dxa')
        myelt=elt.append(myelt)

基于,我想出了这个解决方案:

from docx.oxml import OxmlElement
from docx.oxml.ns import qn

def indent_table(table, indent):
    # noinspection PyProtectedMember
    tbl_pr = table._element.xpath('w:tblPr')
    if tbl_pr:
        e = OxmlElement('w:tblInd')
        e.set(qn('w:w'), str(indent))
        e.set(qn('w:type'), 'dxa')
        tbl_pr[0].append(e)