如何在python-docx中将表行保持在一起?

问题描述 投票:9回答:1

作为一个例子,我有一个通用脚本,使用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”,这成功阻止了表格在页面中拆分:

enter image description here

这是非破坏表的XML代码:

enter image description here

您可以看到突出显示的行显示应该将表保持在一起的段落属性。所以我编写了这个函数并将其粘贴在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标签或阻止它正常工作的东西吗?

python xml pagination python-docx
1个回答
1
投票

好的,我也需要这个。我认为我们都在做错误的假设,即Word的表属性中的设置(或者在python-docx中实现这一点的等效方法)是关于保持表不被跨页分割。它不是 - 相反,它只是关于表的行是否可以跨页分割。

鉴于我们知道在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])
© www.soinside.com 2019 - 2024. All rights reserved.