Skip to content

How to use Python iteration to read paragraphs, tables and pictures in word? #650

@Slowhalfframe

Description

@Slowhalfframe

So far, I've found a way to read paragraphs and tables in word sequentially and iteratively, but I'm stuck with how to read pictures sequentially.
I would like to ask you to help me on the basis of the original code to achieve how the sequence of iteration word pictures?
Here is my current code

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, _Row
from docx.text.paragraph import Paragraph
import docx
path = './test.docx'
doc = docx.Document(path)

def iter_block_items(parent):
    if isinstance(parent, _Document):
        parent_elm = parent.element.body
    elif isinstance(parent, _Cell):
        parent_elm = parent._tc
    elif isinstance(parent, _Row):
        parent_elm = parent._tr
    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)

for block in iter_block_items(doc):
    # read Paragraph
    if isinstance(block, Paragraph):
        print(block.text)
    # read table
    elif isinstance(block, Table):
        print(block.style.name)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions