Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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)

You want to modify iter_block_items such that the iteration body handles the case of a picture: currently it only handles (and yields) paragraphs and tables.

I don't really know the format, but you might want docx.oxml.shape.CT_Picture for the "flag type". Browsing through its documentation, python-docx doesn't seem to have a picture version of the Table or Paragraph wrappers though.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.