python - 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)
我想my_input_field
根据颜色插入,但不知道如何获取/检查/读取它的边框颜色?
恐怕我太笨了,无法处理那里的信息,这对我没有帮助: https://python-pptx.readthedocs.io/en/latest/api/dml.html#pptx.dml.color。颜色格式
有人可以指出我正确的方向吗?
编辑:
我很确定有一种方法可以访问颜色。文档状态:
细胞
单元格具有背景填充、边框、边距和其他几个可以逐个单元格自定义的格式设置。
但我不知道如何访问这个属性。我已经查看了设置颜色的代码片段,但我无法从这些示例中理解任何意义。
编辑 2:我的解决方法
我仍然没有解决方案,但万一有人偶然发现 - 这是我的小解决方法:我将表格的颜色名称作为文本放在表格本身中。
迭代所有表时,我从表中读取此文本并将其删除。这样我可以区分表格并添加正确的信息。
这不是很好,但它确实有效。
解决方案
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)
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
推荐阅读
- javascript - 为什么我的 redux 应用程序没有在 redux-thunk 中缓存异步 api 调用?
- python - 下载 S3 存储桶中子文件夹的全部内容
- reactjs - 不呈现 else 块的条件语句
- javascript - 推送 onClick 时过滤的项目不呈现(反应)
- javascript - 一次显示一个下拉菜单并关闭其他下拉菜单
- c# - 将数字拆分为列表/数组(例如 1998 到 1,9,9,8)
- javascript - 如何处理“阅读更多或更少”使用危险的SetInnerHTML加载内容
- android - CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera) 已过时
- python - 不确定 asyncio 的工作原理以及我如何从持续运行的异步文件夹看门狗函数返回信息
- azure-devops - Azure Devops:chromedriver 不是有效的 Win32 应用程序