python - 使用python为ppt中的表格单元格着色
问题描述
所以我有一个包含这样的表格的excel:
我想使用 Python 在 powerpoint 中获得同一张表
到目前为止完成的工作:
- 将excel读取到python并存储在pandas df中
- 将 df 添加到 powerpoint
相同努力的代码:
from pd2ppt import df_to_table
import pandas as pd
from pptx import Presentation
from pptx.util import Inches
path =r"Sample PPT.pptx"
prs = Presentation(path)
title_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
title.text = "Summary Table"
top = Inches(1.5)
left =Inches(0.25)
width =Inches(9.25)
height= Inches(5.0)
df_to_table(slide, df,left, top, width, height)
我只需要如何使用 Python 在此表中进行颜色格式化?
解决方案
PowerPoint 表格中的每个单元格都有自己的fill,它可以做其他FillFormat
对象可以做的所有事情::
from pptx.dml.color import RGBColor
cell = table.cell(0, 0) # ---or whatever cell you choose---
fill = cell.fill
fill.solid()
fill.fore_color.rgb = RGBColor(0xFA, 0x00, 0x37)
此处的FillFormat
文档中进一步描述了对象接口:
https ://python-pptx.readthedocs.io/en/latest/api/dml.html#fillformat-objects
推荐阅读
- node.js - TypeError:无法读取 node.JS 中未定义的属性“用户名”/快递无法发布到路由
- python - 如何使用 Python 从 Azure Function Http 请求中获取 IP 地址
- c - 实时逆矩阵计算
- python - 如何在 SQLAlchemy 中动态使用 select?
- java - [Java][Spring] 插入查询执行没有错误但没有插入数据
- java - 每个活动都有不同的导航主机片段
- regex - 如何构建正则表达式来搜索具有非字母数字字符的字符串?
- python-3.x - AttributeError:“DirectoryIterator”对象没有属性“地图”
- powerbi - 用于自定义视觉对象的多个不相关的 dataViewMappings Power BI 报表服务器
- php - Laravel - 目标类 [PostsController] 不存在