Python PPTX:获取 table 边框颜色
Python PPTX: Get table border color
我已经使用 python-pptx 读取了 Python 中现有的 .pptx 文件,并且我可以访问 powerpoint 幻灯片中的 tables。
我没能做到:获取单元格的边框颜色。
我想根据 table 的边框颜色在 table 单元格中插入数据,例如。 G。在带有绿色边框的 table 单元格中,插入“111”,在带有红色边框的单元格中插入“222”。
插入值有效,但不检查 tables(或单元格)边框颜色,数据最终出现在错误的位置。
一张ppt幻灯片上有多个table。 tables 都是独特的边框颜色,e。 G。一个 table 周围有纯绿色边框,另一个是完全绿色的,另一个是蓝色的,依此类推。
这就是我迭代页面 table 和访问单元格的方式:
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)
我想根据颜色插入my_input_field
,但不知道如何get/check/read它的边框颜色?
恐怕我太笨了,无法处理那里的信息,这对我没有帮助:
https://python-pptx.readthedocs.io/en/latest/api/dml.html#pptx.dml.color.ColorFormat
有人能指出我正确的方向吗?
编辑:
我很确定有一种方法可以访问颜色。 The docs state:
cell
A cell has a background fill, borders, margins, and several other
formatting settings that can be customized on a cell-by-cell basis.
但我不知道如何访问此属性。我看过设置颜色的代码片段,但我无法理解这些示例。
编辑 2:我的解决方法
我仍然没有解决方案,但万一有人无意中发现了这个 - 这是我的小解决方法:我将 table 的颜色名称放在 table 本身中,作为文本。
在遍历所有 table 时,我从 table 中读取了这段文字并将其删除。这样我就可以区分 table 并添加正确的信息。
它不是很好,但很管用。
python-pptx
目前版本好像不支持cell borders,不过还有个办法:
PPTX 文件使用 XML 并且 python-pptx
可用于访问该文件 XML 并找到您想要的数据(如果您知道在哪里查找)。
我使用了一个简单的演示文稿,只有一个 table 和两个像这样的单元格:
请注意,单元格之间的中间线必须是红色或绿色。
使用这段代码我得到了两个单元格的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)
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>
每行指定颜色:左(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
我已经使用 python-pptx 读取了 Python 中现有的 .pptx 文件,并且我可以访问 powerpoint 幻灯片中的 tables。
我没能做到:获取单元格的边框颜色。
我想根据 table 的边框颜色在 table 单元格中插入数据,例如。 G。在带有绿色边框的 table 单元格中,插入“111”,在带有红色边框的单元格中插入“222”。
插入值有效,但不检查 tables(或单元格)边框颜色,数据最终出现在错误的位置。
一张ppt幻灯片上有多个table。 tables 都是独特的边框颜色,e。 G。一个 table 周围有纯绿色边框,另一个是完全绿色的,另一个是蓝色的,依此类推。
这就是我迭代页面 table 和访问单元格的方式:
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)
我想根据颜色插入my_input_field
,但不知道如何get/check/read它的边框颜色?
恐怕我太笨了,无法处理那里的信息,这对我没有帮助: https://python-pptx.readthedocs.io/en/latest/api/dml.html#pptx.dml.color.ColorFormat
有人能指出我正确的方向吗?
编辑:
我很确定有一种方法可以访问颜色。 The docs state:
cell
A cell has a background fill, borders, margins, and several other formatting settings that can be customized on a cell-by-cell basis.
但我不知道如何访问此属性。我看过设置颜色的代码片段,但我无法理解这些示例。
编辑 2:我的解决方法
我仍然没有解决方案,但万一有人无意中发现了这个 - 这是我的小解决方法:我将 table 的颜色名称放在 table 本身中,作为文本。
在遍历所有 table 时,我从 table 中读取了这段文字并将其删除。这样我就可以区分 table 并添加正确的信息。
它不是很好,但很管用。
python-pptx
目前版本好像不支持cell borders,不过还有个办法:
PPTX 文件使用 XML 并且 python-pptx
可用于访问该文件 XML 并找到您想要的数据(如果您知道在哪里查找)。
我使用了一个简单的演示文稿,只有一个 table 和两个像这样的单元格:
请注意,单元格之间的中间线必须是红色或绿色。
使用这段代码我得到了两个单元格的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)
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>
每行指定颜色:左(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