我想编写一个程序,以获取我的docx文件,对其进行迭代,然后根据标题将每个文件分成多个单独的文件。每个docx内都有几篇文章,每篇文章下方都有一个“标题1”和文字。
因此,如果我的原始file1.docx有4篇文章,我希望将其分成4个单独的文件,每个文件的标题和文本。
我到达了在其中保存.docx文件的路径中迭代所有文件的部分,并且可以分别阅读标题和文本,但似乎无法找出一种方法合并所有内容并将其拆分为单独的文件,每个文件都包含标题和文本。我正在使用python-docx库。
import glob
from docx import Document
headings = []
texts = []
def iter_headings(paragraphs):
for paragraph in paragraphs:
if paragraph.style.name.startswith('Heading'):
yield paragraph
def iter_text(paragraphs):
for paragraph in paragraphs:
if paragraph.style.name.startswith('Normal'):
yield paragraph
for name in glob.glob('/*.docx'):
document = Document(name)
for heading in iter_headings(document.paragraphs):
headings.append(heading.text)
for paragraph in iter_text(document.paragraphs):
texts.append(paragraph.text)
print(texts)
如何提取每篇文章的文字和标题?
这是python-docx给我的XML阅读。红色大括号标记了我要从每个文件中提取的内容。
https://user-images.githubusercontent.com/17858776/51575980-4dcd0200-1eac-11e9-95a8-f643f87b1f40.png
我愿意接受任何其他建议,这些建议涉及如何使用不同的方法来实现我想要的功能,或者是否有更简便的方法来处理PDF文件。
答案 0 :(得分:0)
我认为使用迭代器的方法是一种合理的方法,但是我倾向于将它们打包。在最高级别上,您可以:
for paragraphs in iterate_document_sections(document.paragraphs):
create_document_from_paragraphs(paragraphs)
然后iterate_document_sections()
类似于:
def iterate_document_sections(document):
"""Generate a sequence of paragraphs for each headed section in document.
Each generated sequence has a heading paragraph in its first position,
followed by one or more body paragraphs.
"""
paragraphs = [document.paragraphs[0]]
for paragraph in document.paragraphs[1:]:
if is_heading(paragraph):
yield paragraphs
paragraphs = [paragraph]
continue
paragraphs.append(paragraph)
yield paragraphs
像这样的东西与其他代码的某些部分结合在一起,应该可以为您提供一些可行的开始。您将需要实现is_heading()
和create_document_from_paragraphs()
。
请注意,此处的“节”一词与通常的发布用语一样,用于指代(节)标题及其从属段落,而不是指Word文档的节对象(如document.sections
)。
答案 1 :(得分:0)
事实上,仅当文档除了段落(例如表格)没有任何其他元素时,提供的解决方案才有效。
另一种可能的解决方案是不仅遍历段落,而且遍历所有文档正文的子 xml 元素。一旦找到“子文档”的开始和结束元素(示例中带有标题的段落),您应该删除与此部分无关的其他 xml 元素(一种切断所有其他文档内容的方法)。通过这种方式,您可以保留所有样式、文本、表格和其他文档元素和格式。 这不是一个优雅的解决方案,意味着您必须在内存中保留一份完整源文档的临时副本。
这是我的代码:
import tempfile
from typing import Generator, Tuple, Union
from docx import Document
from docx.document import Document as DocType
from docx.oxml.table import CT_Tbl
from docx.oxml.text.paragraph import CT_P
from docx.oxml.xmlchemy import BaseOxmlElement
from docx.text.paragraph import Paragraph
def iterparts(doc_path:str, skip_first=True, bias:int=0) -> Generator[Tuple[int,DocType],None,None]:
"""Iterate over sub-documents by splitting source document into parts
Split into parts by copying source document and cutting off unrelevant
data.
Args:
doc_path (str): path to source *docx* file
skip_first (bool, optional): skip first split point and wait for
second occurrence. Defaults to True.
bias (int, optional): split point bias. Defaults to 0.
Yields:
Generator[Tuple[int,DocType],None,None]: first element of each tuple
indicates the number of a
sub-document, if number is 0
then there are no sub-documents
"""
doc = Document(doc_path)
counter = 0
while doc:
split_elem_idx = -1
doc_body = doc.element.body
cutted = [doc, None]
for idx, elem in enumerate(doc_body.iterchildren()):
if is_split_point(elem):
if split_elem_idx == -1 and skip_first:
split_elem_idx = idx
else:
cutted = split(doc, idx+bias) # idx-1 to keep previous paragraph
counter += 1
break
yield (counter, cutted[0])
doc = cutted[1]
def is_split_point(element:BaseOxmlElement) -> bool:
"""Split criteria
Args:
element (BaseOxmlElement): oxml element
Returns:
bool: whether the *element* is the beginning of a new sub-document
"""
if isinstance(element, CT_P):
p = Paragraph(element, element.getparent())
return p.text.startswith("Some text")
return False
def split(doc:DocType, cut_idx:int) -> Tuple[DocType,DocType]:
"""Splitting into parts by copying source document and cutting of
unrelevant data.
Args:
doc (DocType): [description]
cut_idx (int): [description]
Returns:
Tuple[DocType,DocType]: [description]
"""
tmpdocfile = write_tmp_doc(doc)
second_part = doc
second_elems = list(second_part.element.body.iterchildren())
for i in range(0, cut_idx):
remove_element(second_elems[i])
first_part = Document(tmpdocfile)
first_elems = list(first_part.element.body.iterchildren())
for i in range(cut_idx, len(first_elems)):
remove_element(first_elems[i])
tmpdocfile.close()
return (first_part, second_part)
def remove_element(elem: Union[CT_P,CT_Tbl]):
elem.getparent().remove(elem)
def write_tmp_doc(doc:DocType):
tmp = tempfile.TemporaryFile()
doc.save(tmp)
return tmp
请注意,您应该根据您的拆分条件定义 is_split_point
方法