是否可以在段落后添加跨越整个页面宽度的单个水平线?
通过使用add_header
这样的行在标题后创建:
document.add_heading('Test', 0)
我需要在文档中的两个段落之间使用这样的一行,所以是否可以单独添加该行?
答案 0 :(得分:1)
python-docx
API尚不支持此功能。
但是,您可以通过在打开的“模板”文档中创建具有此边框设置的段落样式来创建新文档来实现此效果,例如
document = Document('template-with-style.docx')
然后,您可以使用python-docx将该样式应用于该文档中的新段落。
答案 1 :(得分:0)
只需添加
document.add_paragraph("_____________________________________________")
此行的长度可能不够,但是您可以添加它,我希望您已经解决了新用户遇到的问题
答案 2 :(得分:0)
这可以通过使用低级函数的 python-docx API 实现。这是取自a GitHub comment
from docx.oxml.shared import OxmlElement
from docx.oxml.ns import qn
def insertHR(paragraph):
p = paragraph._p # p is the <w:p> XML element
pPr = p.get_or_add_pPr()
pBdr = OxmlElement('w:pBdr')
pPr.insert_element_before(pBdr,
'w:shd', 'w:tabs', 'w:suppressAutoHyphens', 'w:kinsoku', 'w:wordWrap',
'w:overflowPunct', 'w:topLinePunct', 'w:autoSpaceDE', 'w:autoSpaceDN',
'w:bidi', 'w:adjustRightInd', 'w:snapToGrid', 'w:spacing', 'w:ind',
'w:contextualSpacing', 'w:mirrorIndents', 'w:suppressOverlap', 'w:jc',
'w:textDirection', 'w:textAlignment', 'w:textboxTightWrap',
'w:outlineLvl', 'w:divId', 'w:cnfStyle', 'w:rPr', 'w:sectPr',
'w:pPrChange'
)
bottom = OxmlElement('w:bottom')
bottom.set(qn('w:val'), 'single')
bottom.set(qn('w:sz'), '6')
bottom.set(qn('w:space'), '1')
bottom.set(qn('w:color'), 'auto')
pBdr.append(bottom)
这将为段落添加水平线。