检查单元格内容是否包含特定形状

Check if cell content contains specific shape

我有一个 table,其中包含一些带有上线的文本,上线改变了文本的含义,我希望能够确定每个单元格是否包含线条形状。

据我所见,有一个 cell.part.inline_shapes,但它对 table 中的每个单元格给出相同的结果,并且它没有指定实际形状 (line/rectangular/square等)。

例如在下面的 table 中,只有单元格 [1, 0] 包含行

def is_line(shape):
    #TODO implement
    pass

def is_containing_line(cell):
    # TODO: check if shape is in current cell, as cell.part.inline_shapes are the same in every table cell    
    cell_shapes = cell.part.inline_shapes
    return any(is_line(shape) for shape in cell_shapes)


[i for i, cell in enumerate(table.columns[column_index].cells[starting_row:])
if is_containing_line(cell)]

print(cell._tc.xml) 用于包含线条形状的单元格:

<w:tc xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" 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:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
  xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
  <w:tcPr>
    <w:tcW w:w="1734" w:type="dxa" />
    <w:tcBorders>
      <w:top w:val="single" w:sz="12" w:space="0" w:color="000000" />
      <w:bottom w:val="nil" />
    </w:tcBorders>
  </w:tcPr>
  <w:p>
    <w:pPr>
      <w:pStyle w:val="TableParagraph" />
      <w:spacing w:before="10" />
      <w:rPr>
        <w:b/>
        <w:sz w:val="2" />
      </w:rPr>
    </w:pPr>
  </w:p>
  <w:p>
    <w:pPr>
      <w:pStyle w:val="TableParagraph" />
      <w:spacing w:line="20" w:lineRule="exact" w:before="0" />
      <w:ind w:left="755" />
      <w:rPr>
        <w:sz w:val="2" />
      </w:rPr>
    </w:pPr>
    <w:r>
      <w:rPr>
        <w:sz w:val="2" />
      </w:rPr>
      <w:pict>
        <v:group style="width:11.1pt;height:.6pt;mso-position-horizontal-relative:char;mso-position-vertical-relative:line" coordorigin="0,0" coordsize="222,12">
          <v:rect style="position:absolute;left:0;top:0;width:222;height:12" filled="true" fillcolor="#000000" stroked="false">
            <v:fill type="solid" />
          </v:rect>
        </v:group>
      </w:pict>
    </w:r>
    <w:r>
      <w:rPr>
        <w:sz w:val="2" />
      </w:rPr>
    </w:r>
  </w:p>
  <w:p>
    <w:pPr>
      <w:pStyle w:val="TableParagraph" />
      <w:spacing w:before="0" />
      <w:ind w:left="453" w:right="449" />
      <w:jc w:val="center" />
      <w:rPr>
        <w:sz w:val="16" />
      </w:rPr>
    </w:pPr>
    <w:r>
      <w:rPr>
        <w:sz w:val="16" />
      </w:rPr>
      <w:t>EN</w:t>
    </w:r>
  </w:p>
</w:tc>

print(cell._tc.xml) 对于不包含线条形状的单元格:

<w:tc xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" 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:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
  xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
  <w:tcPr>
    <w:tcW w:w="1734" w:type="dxa" />
  </w:tcPr>
  <w:p>
    <w:pPr>
      <w:pStyle w:val="TableParagraph" />
      <w:spacing w:before="55" />
      <w:ind w:left="453" w:right="448" />
      <w:jc w:val="center" />
      <w:rPr>
        <w:sz w:val="16" />
      </w:rPr>
    </w:pPr>
    <w:r>
      <w:rPr>
        <w:sz w:val="16" />
      </w:rPr>
      <w:t>BOUT</w:t>
    </w:r>
  </w:p>
</w:tc>

python-docx 中没有 API 支持。

但是,此函数会告诉您段落中是否存在绘图(内联形状)。请注意,根据 Word 版本,此类项目可能显示为 <w:pict>(位图图像​​)元素而不是 <w:drawing>(矢量艺术)元素:

def has_inline_shape(paragraph):
    """Return True if `paragraph` contains an inline shape."""
    return (
        bool(paragraph._p.xpath(".//w:drawing"))
        or bool(paragraph._p.xpath(".//w:pict"))
    )

您可以将它应用于单元格中的每个段落,以确定该单元格是否包含这样的形状:

def cell_contains_inline_shape(cell):
    """Return True if an inline-shape appears in `cell`."""
    return any(
        has_inline_shape(paragraph)
        for paragraph in cell.paragraphs
    )