【问题标题】:How do you keep table rows together in python-docx?如何在 python-docx 中将表行保持在一起?
【发布时间】:2017-05-25 12:14:02
【问题描述】:

例如,我有一个通用脚本,它使用python-docx 输出默认表格样式(此代码运行良好):

import docx
d=docx.Document()
type_of_table=docx.enum.style.WD_STYLE_TYPE.TABLE

list_table=[['header1','header2'],['cell1','cell2'],['cell3','cell4']]
numcols=max(map(len,list_table))
numrows=len(list_table)

styles=(s for s in d.styles if s.type==type_of_table)


for stylenum,style in enumerate(styles,start=1):
    label=d.add_paragraph('{}) {}'.format(stylenum,style.name))
    label.paragraph_format.keep_with_next=True
    label.paragraph_format.space_before=docx.shared.Pt(18)
    label.paragraph_format.space_after=docx.shared.Pt(0)
    table=d.add_table(numrows,numcols)
    table.style=style
    for r,row in enumerate(list_table):
        for c,cell in enumerate(row):
            table.row_cells(r)[c].text=cell


d.save('tablestyles.docx')   

接下来,我打开了文档,突出显示了一个拆分表,在段落格式下,选择了“Keep with next”,这成功地防止了表格被拆分到一个页面:

这里是非断表的XML代码:

您可以看到突出显示的行显示了应该将表格保持在一起的段落属性。于是我写了这个函数,并把它卡在d.save('tablestyles.docx')这一行上面的代码中:

def no_table_break(document):
    tags=document.element.xpath('//w:p')
    for tag in tags:
        ppr=tag.get_or_add_pPr()
        ppr.keepNext_val=True


no_table_break(d)     

当我检查 XML 代码时,段落属性标记设置正确,当我打开 Word 文档时,所有表格的“保留下一个”框都被选中,但表格仍然跨页拆分。我是否缺少 XML 标记或阻止其正常工作的东西?

【问题讨论】:

  • 我认为您需要更具体地了解“孤立”行是什么。下一步将是查看您是否可以完成使用 Word 应用程序/UI 后的结果。如果您可以通过这种方式缩小范围,您可以确定产生差异的 XML 元素/属性。 w:cantSplit 可以确定一个单元格是否跨页拆分(当然还有它的行)。
  • @scanny 我所说的孤立行是表的一部分在一个页面上,而表的另一部分在另一个页面上。
  • 问题是中断是在偶数行边界上还是在行内中断,就像一页上一行的一部分,而其余部分在下一页的顶部。这些是不同的(错误)行为。
  • @scanny 在我的情况下,中断是在偶数行边界上,而不是环绕行。
  • 您能否使用 Word 应用程序完成您正在寻找的结果?如果是这样,你做了什么有效的?喜欢什么对话框选项或其他什么?

标签: python xml pagination python-docx


【解决方案1】:

好的,我也需要这个。我认为我们都做出了错误的假设,即 Word 的表格属性中的设置(或在 python-docx 中实现此目的的等效方法)是为了防止 table 被跨页面拆分。这不是 - 相反,它只是关于表的 是否可以跨页面拆分。

鉴于我们知道如何在 python-docx 中成功地做到这一点,我们可以通过将每个表放在更大的主表的行中来防止表被拆分到页面中。下面的代码成功地做到了这一点。我正在使用 Python 3.6 和 Python-Docx 0.8.6

import docx
from docx.oxml.shared import OxmlElement
import os
import sys


def prevent_document_break(document):
    """https://github.com/python-openxml/python-docx/issues/245#event-621236139
       Globally prevent table cells from splitting across pages.
    """
    tags = document.element.xpath('//w:tr')
    rows = len(tags)
    for row in range(0, rows):
        tag = tags[row]  # Specify which <w:r> tag you want
        child = OxmlElement('w:cantSplit')  # Create arbitrary tag
        tag.append(child)  # Append in the new tag


d = docx.Document()
type_of_table = docx.enum.style.WD_STYLE_TYPE.TABLE

list_table = [['header1', 'header2'], ['cell1', 'cell2'], ['cell3', 'cell4']]
numcols = max(map(len, list_table))
numrows = len(list_table)

styles = (s for s in d.styles if s.type == type_of_table)

big_table = d.add_table(1, 1)
big_table.autofit = True

for stylenum, style in enumerate(styles, start=1):
    cells = big_table.add_row().cells
    label = cells[0].add_paragraph('{}) {}'.format(stylenum, style.name))
    label.paragraph_format.keep_with_next = True
    label.paragraph_format.space_before = docx.shared.Pt(18)
    label.paragraph_format.space_after = docx.shared.Pt(0)

    table = cells[0].add_table(numrows, numcols)
    table.style = style
    for r, row in enumerate(list_table):
        for c, cell in enumerate(row):
            table.row_cells(r)[c].text = cell

prevent_document_break(d)

d.save('tablestyles.docx')

# because I'm lazy...
openers = {'linux': 'libreoffice tablestyles.docx',
           'linux2': 'libreoffice tablestyles.docx',
           'darwin': 'open tablestyles.docx',
           'win32': 'start tablestyles.docx'}
os.system(openers[sys.platform])

【讨论】:

    【解决方案2】:

    已经为这个问题苦苦挣扎了几个小时,终于发现解决方案对我来说很好用。我刚刚更改了主题启动器代码中的 XPath,所以现在它看起来像这样:

    def keep_table_on_one_page(doc):
        tags = self.doc.element.xpath('//w:tr[position() < last()]/w:tc/w:p')
        for tag in tags:
            ppr = tag.get_or_add_pPr()
            ppr.keepNext_val = True
    

    关键时刻是这个选择器

    [position() < last()]
    

    我们希望每个表中除了最后一行之外的所有行都与下一行保持一致

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 2012-06-23
      • 2017-02-09
      • 2022-01-20
      • 2010-09-20
      相关资源
      最近更新 更多