例如,我有一个使用 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')
接下来,我打开文档,突出显示拆分表格并在段落格式下选择“与下一个保持一致”,这成功地防止了表格被拆分成一页:
这里是非破表的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 表格属性中的设置(或在 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])
关于python - 你如何在 python-docx 中将表行保持在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41578438/