was*_*256 3 python python-docx
是否可以在段落后添加一条横跨整个页面宽度的水平线?
通过使用,add_header在标题后创建这样的行:
document.add_heading('Test', 0)
Run Code Online (Sandbox Code Playgroud)
我需要在文档中的两个段落之间添加这样一条线,那么是否可以单独添加该行?
这可以通过使用低级函数的 python-docx API 来实现。这摘自GitHub 评论
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)
Run Code Online (Sandbox Code Playgroud)
这将为段落添加水平线。
目前尚不直接支持这一点python-docx。
但是,您可以通过在打开创建新文档的“模板”文档中创建具有此边框设置的段落样式来实现此效果,例如
document = Document('template-with-style.docx')
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用 python-docx 将该样式应用到该文档中的新段落。
| 归档时间: |
|
| 查看次数: |
10500 次 |
| 最近记录: |