有什么方法可以读取 .docx 文件包括使用 python-docx 的自动编号
Is there any way to read .docx file include auto numbering using python-docx
问题陈述:从 .docx 文件中提取部分,包括自动编号。
我试过 python-docx 从 .docx 文件中提取文本,但它不包括自动编号。
from docx import Document
document = Document("wadali.docx")
def iter_items(paragraphs):
for paragraph in document.paragraphs:
if paragraph.style.name.startswith('Agt'):
yield paragraph
if paragraph.style.name.startswith('TOC'):
yield paragraph
if paragraph.style.name.startswith('Heading'):
yield paragraph
if paragraph.style.name.startswith('Title'):
yield paragraph
if paragraph.style.name.startswith('Heading'):
yield paragraph
if paragraph.style.name.startswith('Table Normal'):
yield paragraph
if paragraph.style.name.startswith('List'):
yield paragraph
for item in iter_items(document.paragraphs):
print item.text
看来目前python-docx v0.8 还没有完全支持编号。你需要做一些黑客攻击。
首先,为了演示,要迭代文档段落,您需要编写自己的迭代器。
这是一些实用的东西:
import docx.document
import docx.oxml.table
import docx.oxml.text.paragraph
import docx.table
import docx.text.paragraph
def iter_paragraphs(parent, recursive=True):
"""
Yield each paragraph and table child within *parent*, in document order.
Each returned value is an instance of Paragraph. *parent*
would most commonly be a reference to a main Document object, but
also works for a _Cell object, which itself can contain paragraphs and tables.
"""
if isinstance(parent, docx.document.Document):
parent_elm = parent.element.body
elif isinstance(parent, docx.table._Cell):
parent_elm = parent._tc
else:
raise TypeError(repr(type(parent)))
for child in parent_elm.iterchildren():
if isinstance(child, docx.oxml.text.paragraph.CT_P):
yield docx.text.paragraph.Paragraph(child, parent)
elif isinstance(child, docx.oxml.table.CT_Tbl):
if recursive:
table = docx.table.Table(child, parent)
for row in table.rows:
for cell in row.cells:
for child_paragraph in iter_paragraphs(cell):
yield child_paragraph
您可以使用它来查找所有文档段落,包括 table 个单元格中的段落。
例如:
import docx
document = docx.Document("sample.docx")
for paragraph in iter_paragraphs(document):
print(paragraph.text)
要访问编号 属性,您需要在 "protected" 成员 paragraph._p.pPr.numPr
中搜索,这是一个 docx.oxml.numbering.CT_NumPr
对象:
for paragraph in iter_paragraphs(document):
num_pr = paragraph._p.pPr.numPr
if num_pr is not None:
print(num_pr) # type: docx.oxml.numbering.CT_NumPr
请注意,此对象是从 numbering.xml
文件(在 docx 内)中提取的(如果存在)。
要访问它,您需要像阅读包一样阅读您的 docx 文件。例如:
import docx.package
import docx.parts.document
import docx.parts.numbering
package = docx.package.Package.open("sample.docx")
main_document_part = package.main_document_part
assert isinstance(main_document_part, docx.parts.document.DocumentPart)
numbering_part = main_document_part.numbering_part
assert isinstance(numbering_part, docx.parts.numbering.NumberingPart)
ct_numbering = numbering_part._element
print(ct_numbering) # CT_Numbering
for num in ct_numbering.num_lst:
print(num) # CT_Num
print(num.abstractNumId) # CT_DecimalNumber
Office Open XMl 文档中提供了更多信息。
有一个包 docx2python 以更简单的方式执行此操作:pypi。org/project/docx2python/
以下代码:
from docx2python import docx2python
document = docx2python("C:/input/MyDoc.docx")
print(document.body)
生成一个列表,其中包含以可解析的方式包含项目符号列表的内容。
问题陈述:从 .docx 文件中提取部分,包括自动编号。
我试过 python-docx 从 .docx 文件中提取文本,但它不包括自动编号。
from docx import Document
document = Document("wadali.docx")
def iter_items(paragraphs):
for paragraph in document.paragraphs:
if paragraph.style.name.startswith('Agt'):
yield paragraph
if paragraph.style.name.startswith('TOC'):
yield paragraph
if paragraph.style.name.startswith('Heading'):
yield paragraph
if paragraph.style.name.startswith('Title'):
yield paragraph
if paragraph.style.name.startswith('Heading'):
yield paragraph
if paragraph.style.name.startswith('Table Normal'):
yield paragraph
if paragraph.style.name.startswith('List'):
yield paragraph
for item in iter_items(document.paragraphs):
print item.text
看来目前python-docx v0.8 还没有完全支持编号。你需要做一些黑客攻击。
首先,为了演示,要迭代文档段落,您需要编写自己的迭代器。 这是一些实用的东西:
import docx.document
import docx.oxml.table
import docx.oxml.text.paragraph
import docx.table
import docx.text.paragraph
def iter_paragraphs(parent, recursive=True):
"""
Yield each paragraph and table child within *parent*, in document order.
Each returned value is an instance of Paragraph. *parent*
would most commonly be a reference to a main Document object, but
also works for a _Cell object, which itself can contain paragraphs and tables.
"""
if isinstance(parent, docx.document.Document):
parent_elm = parent.element.body
elif isinstance(parent, docx.table._Cell):
parent_elm = parent._tc
else:
raise TypeError(repr(type(parent)))
for child in parent_elm.iterchildren():
if isinstance(child, docx.oxml.text.paragraph.CT_P):
yield docx.text.paragraph.Paragraph(child, parent)
elif isinstance(child, docx.oxml.table.CT_Tbl):
if recursive:
table = docx.table.Table(child, parent)
for row in table.rows:
for cell in row.cells:
for child_paragraph in iter_paragraphs(cell):
yield child_paragraph
您可以使用它来查找所有文档段落,包括 table 个单元格中的段落。
例如:
import docx
document = docx.Document("sample.docx")
for paragraph in iter_paragraphs(document):
print(paragraph.text)
要访问编号 属性,您需要在 "protected" 成员 paragraph._p.pPr.numPr
中搜索,这是一个 docx.oxml.numbering.CT_NumPr
对象:
for paragraph in iter_paragraphs(document):
num_pr = paragraph._p.pPr.numPr
if num_pr is not None:
print(num_pr) # type: docx.oxml.numbering.CT_NumPr
请注意,此对象是从 numbering.xml
文件(在 docx 内)中提取的(如果存在)。
要访问它,您需要像阅读包一样阅读您的 docx 文件。例如:
import docx.package
import docx.parts.document
import docx.parts.numbering
package = docx.package.Package.open("sample.docx")
main_document_part = package.main_document_part
assert isinstance(main_document_part, docx.parts.document.DocumentPart)
numbering_part = main_document_part.numbering_part
assert isinstance(numbering_part, docx.parts.numbering.NumberingPart)
ct_numbering = numbering_part._element
print(ct_numbering) # CT_Numbering
for num in ct_numbering.num_lst:
print(num) # CT_Num
print(num.abstractNumId) # CT_DecimalNumber
Office Open XMl 文档中提供了更多信息。
有一个包 docx2python 以更简单的方式执行此操作:pypi。org/project/docx2python/
以下代码:
from docx2python import docx2python
document = docx2python("C:/input/MyDoc.docx")
print(document.body)
生成一个列表,其中包含以可解析的方式包含项目符号列表的内容。