如何使用python-docx将行号添加到docx文档部分 [英] How to add line numbers to a docx document section using python-docx
问题描述
我正在使用 python-docx 生成一些文档.
我可以看到存在一个行编号属性,该属性可以应用于文档部分(对于至少是 OOXML 标准).
我还可以看到 python-docx API .
我假设可以访问基础的 sectPr
字段来添加 lnNumType
标记,但是我无法(轻松地)找到任何示例./p>
我对我的标准感到困惑吗?还是我的问题有点晦涩?
一旦有了Section对象,就可以使用以下方法获取sectPr元素:
sectPr = section._sectPr
如果您使用'python-docx解决方法函数OxmlElement'来搜索Google,则会找到示例.所有元素都继承自lxml _Element,因此可以进行lxml操作.BaseOxmlElement还添加了一些方便的其他方法.一个基本要点是:
sectPr = section._sectPrlnNumType = OxmlElement('w:lnNumType')lnNumType.set('fooAttrib','42')sectPr.append(lnNumType)
在很多情况下,您需要按正确的顺序来获取任何新的子元素,因为几乎总是规定了顺序.
您可以在这里找到对 w:sectPr
元素的一些便捷分析: http://python-docx.readthedocs.io/en/latest/dev/analysis/features/sections.html
乍一看,您可以在其末尾附加 w:lnNumType
,因为后面的元素不太常见.但是,如果您想更加严格,可以使用它代替 sectPr.append()
:
sectPr.insert_element_before(lnNumType,('w:pgNumType','w:pgNumType','w:cols','w:formProt','w:vAlign','w:noEndnote','w:titlePg','w:textDirection','w:bidi','w:rtlGutter','w:docGrid','w:printerSettings','w:sectPrChange',))
您可以在此处查看 .insert_element_before()
的实现: https://github.com/python-openxml/python-docx/blob/master/docx/oxml/xmlchemy.py#L718
I am using python-docx to generate some documents.
I can see that there exists a line numbering property which may be applied to document sections (for the OOXML standard at least).
I can also see that this property is not present in the python-docx API.
I am assuming it is possible to access the underlying sectPr
field to add the lnNumType
tag, but I have not been able to (easily) find any examples.
Am I getting my standards confused? Or is my question just a bit obscure?
Once you have the Section object, you can get the sectPr element with:
sectPr = section._sectPr
If you Google on 'python-docx workaround function OxmlElement' you'll find examples. All elements inherit from lxml _Element so lxml manipulation works. There are also some handy other methods added by BaseOxmlElement. A basic gist would be:
sectPr = section._sectPr
lnNumType = OxmlElement('w:lnNumType')
lnNumType.set('fooAttrib', '42')
sectPr.append(lnNumType)
In many cases you'll need to attend to getting any new child elements in the right order, as the sequence is almost always prescribed.
You can find some handy analysis of the w:sectPr
element here:
http://python-docx.readthedocs.io/en/latest/dev/analysis/features/sections.html
It looks from a quick glance that you'll be able to just append a w:lnNumType
on the end as the elements that follow it are less common. But if you wanted to be more rigorous you could use this instead of sectPr.append()
:
sectPr.insert_element_before(lnNumType, (
'w:pgNumType', 'w:pgNumType', 'w:cols', 'w:formProt', 'w:vAlign',
'w:noEndnote', 'w:titlePg', 'w:textDirection', 'w:bidi',
'w:rtlGutter', 'w:docGrid', 'w:printerSettings', 'w:sectPrChange',
))
You can see the implementation for .insert_element_before()
here:
https://github.com/python-openxml/python-docx/blob/master/docx/oxml/xmlchemy.py#L718
这篇关于如何使用python-docx将行号添加到docx文档部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!