Python DocX文件阅读——需要识别和区分文本、表格和图像

问题描述 投票:0回答:2

我有一些随机的 docx 文件。它可以包含纯文本、图像或表格。我需要浏览文档并确定哪个段落包含什么。如果是文本就打印它,如果是图片就打印IMAGE,如果是表格就打印它的内容。我可以为文本做这件事,这很容易,但是以某种方式识别图片是行不通的,关于表格我只能确定有一个,但不能打印它的内容。 有人可以帮忙吗?

from docx import Document

doc = Document('SomeDoc.docx')
pars = doc.paragraphs
for p in pars:
    print(p.text)
...????

我试过 XML 转储,这是一种方法,但我想用 docx 模块解决它。与 doc2txt 模块相同,适用于文本和表格,但不适用于图像。

python python-docx
2个回答
0
投票

您可以使用段落的

._element
属性并检查它是否包含图像或表格的XML 标记。这是一个演示如何执行此操作的示例代码片段:

from docx import Document
from docx.oxml import OxmlElement

def identify_content(paragraph):
    if isinstance(paragraph._element, OxmlElement) and paragraph._element.xml.startswith('<w:pict'):
        print('IMAGE')
    elif isinstance(paragraph._element, OxmlElement) and paragraph._element.xml.startswith('<w:tbl'):
        table = paragraph._element._element
        for row in table.findall('.//w:tr'):
            for cell in row.findall('.//w:t'):
                print(cell.text)
    else:
        print(paragraph.text)

doc = Document('SomeDoc.docx')
for paragraph in doc.paragraphs:
    identify_content(paragraph)

identify_content()
函数将段落对象作为输入并检查它是否包含图像或表格。如果它包含图像,它会打印“IMAGE”。如果它包含一个表格,它会从表格的每个单元格中提取文本并打印出来。否则,它打印段落的文本。


0
投票

我试图修改你的代码 - 这有效:

from docx import Document
from docx.oxml.text.paragraph import CT_P

    def identify_content(paragraph):
        if isinstance(paragraph._element, CT_P) and paragraph._element.xml.startswith('<w:pict'):
            print('IMAGE')
        elif isinstance(paragraph._element, CT_P) and paragraph._element.xml.startswith('<w:tbl'):
            table = paragraph._element._element
            for row in table.findall('.//w:tr'):
                for cell in row.findall('.//w:t'):
                    print(cell.text)
        else:
            print(paragraph.text)
    
    doc = Document('SomeDoc.docx')
    for paragraph in doc.paragraphs:
        identify_content(paragraph)

但是还是找不到图片和表格。元素名称是否正确? 来自文件的 XML 在这里:

    <w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
  <w:p>
    <w:pPr>
      <w:jc w:val="left"/>
    </w:pPr>
    <w:r>
      <w:drawing>
        <wp:inline xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
          <wp:extent cx="5257800" cy="1212834"/>
          <wp:docPr id="1" name="Picture 1"/>
          <wp:cNvGraphicFramePr>
            <a:graphicFrameLocks noChangeAspect="1"/>
          </wp:cNvGraphicFramePr>
          <a:graphic>
            <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
              <pic:pic>
                <pic:nvPicPr>
                  <pic:cNvPr id="0" name="logo.png"/>
                  <pic:cNvPicPr/>
                </pic:nvPicPr>
                <pic:blipFill>
                  <a:blip r:embed="rId9"/>
                  <a:stretch>
                    <a:fillRect/>
                  </a:stretch>
                </pic:blipFill>
                <pic:spPr>
                  <a:xfrm>
                    <a:off x="0" y="0"/>
                    <a:ext cx="5257800" cy="1212834"/>
                  </a:xfrm>
                  <a:prstGeom prst="rect"/>
                </pic:spPr>
              </pic:pic>
            </a:graphicData>
          </a:graphic>
        </wp:inline>
      </w:drawing>
    </w:r>
  </w:p>
  <w:p>
    <w:pPr>
      <w:pStyle w:val="Title"/>
    </w:pPr>
    <w:r>
      <w:t>Faktura</w:t>
    </w:r>
  </w:p>
  <w:p>
    <w:r>
      <w:rPr>
        <w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/>
        <w:sz w:val="28"/>
      </w:rPr>
      <w:t xml:space="preserve">Milý/á </w:t>
    </w:r>
    <w:r>
      <w:rPr>
        <w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/>
        <w:b/>
        <w:sz w:val="28"/>
      </w:rPr>
      <w:t>Honzo,</w:t>
    </w:r>
  </w:p>
  <w:p>
    <w:r>
      <w:rPr>
        <w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/>
        <w:sz w:val="28"/>
      </w:rPr>
      <w:t>posíláme Vám fakturu za poslední objednávku. Její souhrn najdete v přiložené tabulce níže.</w:t>
    </w:r>
  </w:p>
  <w:p/>
  <w:p/>
  <w:tbl>
    <w:tblPr>
      <w:tblW w:type="auto" w:w="0"/>
      <w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="0" w:noHBand="0" w:noVBand="1" w:val="04A0"/>
    </w:tblPr>
    <w:tblGrid>
      <w:gridCol w:w="2160"/>
      <w:gridCol w:w="2160"/>
      <w:gridCol w:w="2160"/>
      <w:gridCol w:w="2160"/>
    </w:tblGrid>
    <w:tr>
      <w:tc>
        <w:tcPr>
          <w:tcW w:type="dxa" w:w="2160"/>
        </w:tcPr>
        <w:p>
          <w:r>
            <w:rPr>
              <w:b/>
              <w:sz w:val="28"/>
            </w:rPr>
            <w:t>Produkt</w:t>
          </w:r>
        </w:p>
      </w:tc>
      <w:tc>
        <w:tcPr>
          <w:tcW w:type="dxa" w:w="2160"/>
        </w:tcPr>
        <w:p>
          <w:r>
            <w:rPr>
              <w:b/>
              <w:sz w:val="28"/>
            </w:rPr>
            <w:t>Množství</w:t>
          </w:r>
        </w:p>
      </w:tc>
      <w:tc>
        <w:tcPr>
          <w:tcW w:type="dxa" w:w="2160"/>
        </w:tcPr>
        <w:p>
          <w:r>
            <w:rPr>
              <w:b/>
              <w:sz w:val="28"/>
            </w:rPr>
            <w:t>Jednotková cena</w:t>
          </w:r>
        </w:p>
      </w:tc>
      <w:tc>
        <w:tcPr>
          <w:tcW w:type="dxa" w:w="2160"/>
        </w:tcPr>
        <w:p>
          <w:r>
            <w:rPr>
              <w:b/>
              <w:sz w:val="28"/>
            </w:rPr>
            <w:t>Celková cena</w:t>
          </w:r>
        </w:p>
      </w:tc>
    </w:tr>
    <w:tr>
      <w:tc>
        <w:tcPr>
          <w:tcW w:type="dxa" w:w="2160"/>
        </w:tcPr>
        <w:p>
          <w:r>
            <w:rPr>
              <w:sz w:val="24"/>
            </w:rPr>
            <w:t>Python - Akreditovaný rekvalifikační kurz</w:t>
          </w:r>
        </w:p>
      </w:tc>
      <w:tc>
        <w:tcPr>
          <w:tcW w:type="dxa" w:w="2160"/>
        </w:tcPr>
        <w:p>
          <w:r>
            <w:rPr>
              <w:sz w:val="24"/>
            </w:rPr>
            <w:t>1.00</w:t>
          </w:r>
        </w:p>
      </w:tc>
      <w:tc>
        <w:tcPr>
          <w:tcW w:type="dxa" w:w="2160"/>
        </w:tcPr>
        <w:p>
          <w:r>
            <w:rPr>
              <w:sz w:val="24"/>
            </w:rPr>
            <w:t>34900.00</w:t>
          </w:r>
        </w:p>
      </w:tc>
      <w:tc>
        <w:tcPr>
          <w:tcW w:type="dxa" w:w="2160"/>
        </w:tcPr>
        <w:p>
          <w:r>
            <w:rPr>
              <w:sz w:val="24"/>
            </w:rPr>
            <w:t>34900.00</w:t>
          </w:r>
        </w:p>
      </w:tc>
    </w:tr>
  </w:tbl>
  <w:p/>
  <w:p/>
  <w:p/>
  <w:p/>
  <w:p/>
  <w:p/>
  <w:p>
    <w:r>
      <w:rPr>
        <w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/>
        <w:sz w:val="28"/>
      </w:rPr>
      <w:t>Děkujeme Vám za využití našich služeb. Velmi rádi Vás uvidíme znovu!</w:t>
      <w:br/>
      <w:t>S pozdravem,</w:t>
      <w:br/>
      <w:t>Jakub</w:t>
    </w:r>
  </w:p>
  <w:sectPr w:rsidR="00FC693F" w:rsidRPr="0006063C" w:rsidSect="00034616">
    <w:pgSz w:w="12240" w:h="15840"/>
    <w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="720" w:footer="720" w:gutter="0"/>
    <w:cols w:space="720"/>
    <w:docGrid w:linePitch="360"/>
  </w:sectPr>
</w:body>
© www.soinside.com 2019 - 2024. All rights reserved.