Python PPTX:获取表格边框颜色

xph*_*xph 0 python powerpoint python-pptx

我已经设法使用python-pptx读取 Python 中现有的 .pptx 文件,并且可以访问 powerpoint 幻灯片中的表格。

我失败的是:获取单元格的边框颜色。

我想根据表格边框颜色在表格单元格中插入数据,例如在具有绿色边框的表格单元格中插入“111”,在具有红色边框的单元格中插入“222”。

插入值是可行的,但如果不检查表格(或单元格)边框颜色,数据最终会出现在错误的位置。

一张ppt幻灯片上不止一张表格。这些表格均具有独特的边框颜色,例如,一张表格周围有实心绿色边框,另一张表格是全绿色的,另一张表格是蓝色的,等等。

这就是我迭代页表和访问单元格的方式:

from pptx import Presentation

pptx_file = r"my_file_here"
with open(pptx_file, "rb") as pptx:
    prs = Presentation(pptx)
    for slide in prs.slides:
        for shape in slide.shapes:
            if not shape.has_table:
                continue
            table = shape.table

            my_input_field = table.cell(0, 1)
Run Code Online (Sandbox Code Playgroud)

my_input_field我想根据颜色插入,但不知道如何获取/检查/读取它的边框颜色?

恐怕我太愚蠢了,无法处理那里的信息,这对我没有帮助: https: //python-pptx.readthedocs.io/en/latest/api/dml.html#pptx.dml.color。颜色格式

有人能指出我正确的方向吗?

编辑:

我很确定有一种方法可以访问颜色。文档指出

细胞

单元格具有背景填充、边框、边距和其他几个可以逐个单元格自定义的格式设置。

但我不知道如何访问这个属性。我已经查看了设置颜色的代码片段,但我无法从这个示例中理解任何意义。

编辑2:我的解决方法

我仍然没有解决方案,但万一有人偶然发现这一点 - 这是我的小解决方法:我将表格的颜色名称作为文本放在表格本身中。

迭代所有表时,我从表中读取此文本并将其删除。这样我就可以区分表格并添加正确的信息。

这不是很好,但是很有效。

Her*_*cón 5

python-pptx当前版本似乎不支持单元格边框,但有一个替代方案:

PPTX 文件使用 XML,python-pptx如果您知道在哪里查找,则可用于访问该 XML 并查找所需的数据。

我使用了一个简单的演示文稿,只有一个表格和两个单元格,如下所示:

红色和绿色细胞

请注意,单元格之间的中线必须是红色或绿色。

使用这段代码,我获得了两个单元格的 XML:

# Here goes the code of the question

# Iterate over the rows of the table
for i, row in enumerate(table.rows):
    # Iterate over the cells of the row
    for j, cell in enumerate(row.cells):
        # Get the table cell properties (tcPr) from the table cell (tc)
        tcPr = cell._tc.get_or_add_tcPr()
        print(f"XML of tcPR of cell ({i}, {j}):")
        print(tcPr.xml)
Run Code Online (Sandbox Code Playgroud)

XML:

<a:tcPr xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
  <a:lnL w="12700" cap="flat" cmpd="sng" algn="ctr">
    <a:solidFill>
      <a:srgbClr val="FF0000"/> <!-- This is the color (RGB in hex format) -->
    </a:solidFill>
    <a:prstDash val="solid"/>
    <a:round/>
    <a:headEnd type="none" w="med" len="med"/>
    <a:tailEnd type="none" w="med" len="med"/>
  </a:lnL>
  <a:lnR w="12700" cap="flat" cmpd="sng" algn="ctr">
    <!-- Omitted for brevity -->
  </a:lnR>
  <a:lnT w="12700" cap="flat" cmpd="sng" algn="ctr">
    <!-- Omitted for brevity -->
  </a:lnT>
  <a:lnB w="12700" cap="flat" cmpd="sng" algn="ctr">
    <!-- Omitted for brevity -->
  </a:lnB>
</a:tcPr>

<a:tcPr xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
  <!-- Omitted for brevity -->
</a:tcPr>
Run Code Online (Sandbox Code Playgroud)

为每行指定颜色:左 ( lnL)、右 ( lnR)、上 ( lnT)、下 ( lnB)。

要获取其中一根线的颜色,您可以使用XPath

# Here goes the code of the question

# Iterate over the rows of the table
for i, row in enumerate(table.rows):
    # Iterate over the cells of the row
    for j, cell in enumerate(row.cells):
        # Get the table cell properties (tcPr) from the table cell (tc)
        tcPr = cell._tc.get_or_add_tcPr()
        # Use XPath to find the color
        result = tcPr.xpath("./a:lnL/a:solidFill/a:srgbClr/@val")
        # results is a list
        # Get the first element if it exists
        left_line_color = result[0] if result else None
        print(f"Left line color of cell ({i}, {j}): {left_line_color}")

# Output:
# Left line color of cell (0, 0): FF0000
# Left line color of cell (0, 1): 00FF00
Run Code Online (Sandbox Code Playgroud)

Colab 中的演示