顺序遍历doc文档的核心代码如下:

from docx import Document
from docx.document import Document as _Document
from docx.oxml.text.paragraph import CT_P
from docx.oxml.table import CT_Tbl
from docx.table import _Cell, Table
from docx.text.paragraph import Paragraph
   Generate a reference to each paragraph and table child within *parent*,
   in document order. Each returned value is an instance of either Table or
   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.
def iter_block_items(parent):
    if isinstance(parent, _Document):
        parent_elm = parent.element.body
    elif isinstance(parent, _Cell):
        parent_elm = parent._tc
    else:
        raise ValueError("something's not right")
    for child in parent_elm.iterchildren():
        if isinstance(child, CT_P):
            yield Paragraph(child, parent)
        elif isinstance(child, CT_Tbl):
            yield Table(child, parent)
main function to extract tables
def extract_tables(document):
    count = 0
    current_context=''
    #iterator the blocks in doc
    for block in iter_block_items(document):
        # print(block.text if isinstance(block, Paragraph) else '<table>')
        if isinstance(block, Paragraph):
            # print("------------------text--------------------")
            print("text:  " + block.text)
        elif isinstance(block, Table):
            current_context=''
            for row in block.rows:
                row_data = []
                for cell in row.cells:
                    text_cell=''
                    for paragraph in cell.paragraphs:
                        text_cell += paragraph.text.strip()
                    if text_cell is '':
                        text_cell="NULL"
                    row_data.append(text_cell)
                print("|".join(row_data))
if __name__ == '__main__':
    document = Document('./xxxxx.docx')
    extract_tables(document)

以上代码核心思想是顺序取出docx中的每个block然后判断该block是table还是paragraph对象,如果是table在解析table,将里面的内容按行输出。

因为要处理中文,所以在这里使用 python3(相对 python2 编码问题较少)。安装 docx :使用 pip3 install python- docx 如果安装失败可以尝试:pip3 easy-install python- docx docx 文档 结构分为3层:Document对象表示整个 文档 Document包含了Paragraph对象的列表,Paragraph对象用来表示段落一个Paragrap... docx 文件是一个包(WordprocessingML Package),其中包含许多个xml文件,另外还包含其他媒体文件,例如图像、视频。可以将 docx 文件重命名为zip文件,再解压缩,即可看到文件结构。 在日常工作中,有很多简单重复的劳动其实完全可以交给Python程序,比如根据样板文件(模板文件)批量的生成很多个Word文件或PowerPoint文件。Word是微软公司开发的文字处理程序,相信大家都不陌生,日常办公中很多正式的 文档 都是用Word进行撰写和编辑的,目前使用的Word文件后缀名一般为. docx 。PowerPoint是微软公司开发的演示文稿程序,是微软的Office系列软件中的一员,被商业人士、教师、学生等群体广泛使用,通常也将其称之为“幻灯片”。... 由于工作需要提取一个word 文档 中的表格,及其所在的章节,普通的Document.paragraphs 和Document.tables无法满足需求。所以综合GitHub作者的代码及我自己的需求代码如下: from docx .document import Document from docx .oxml.table import CT_Tbl from docx .oxml.text.parag... 最近腾讯开放平台上架管理的比较严,需要软件著作权,申请软件著作权又需要五万行项目代码,想想就头大,正好最近在学习Python,好歹也是个程序员,这种重复性的工作,当然是要用程序解决咯,就写了一个 遍历 项目包下java文件并写入一个word文件的小脚本,脚本很简单,一个Python熟手应该不到二十分钟就能写完,但是自己写了快一个上午,所以在这里把自己的思考问题解决问题的整个过程记录下来,方便自己回顾复... python- docx 还没有对此提供API支持;有趣的是,Microsoft Word API也没有。在但是你可以用下面的代码。请注意,它有点脆弱,因为它使用了python- docx 内部构件,这些内部构件可能会发生变化,但我预计它在可预见的未来会很好地工作:#!/usr/bin/env python# encoding: utf-8"""Testing iter_block_items()"""f... Python 顺序 读取word 文档 中的文本与表格 在网上找了好久都没有找到 顺序 读取word中表格和文本的发方法,看源码看着看着就被绕晕了,最后在某篇博客的评论区里找到了这个代码的链接,原网站也是在评论区里不断讨论完善了代码,貌似中间还经历了python- docx 模块的更新,放这里希望有需要的人可以省得找了,真的不好找啊!!! import os import docx from docx .d... 这是我第一次在这里发表文章,我想写一个脚本,以 docx 作为输入,并选择某些段落(包括表格和图像)以相同的 顺序 复制到另一个模板 文档 中(而不是最后)。我遇到的问题是,当我开始迭代元素时,我的代码无法检测到图像,因此我无法确定图像相对于文本和表格的位置,也无法确定它是哪个图像。简而言之,我得到了doc1:文本图像文本表文本最后我要说的是:文本[图像丢失]文本表文本到目前为止我得到的结果是:-我可以重复段...