如何使用Python-PPTX填充文本框背景颜色?

发布于 2025-01-23 21:07:40 字数 839 浏览 2 评论 0原文

我正在使用Python-PPTX创建PPT,我想用其他颜色填充文本框背景。

from pptx import Presentation
from pptx.util import Inches, Pt
from datetime import datetime
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN

prs = Presentation()
prs.slide_width = Inches(16)
prs.slide_height = Inches(9)
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

slide_layout = prs.slide_layouts[5]
slide2 = prs.slides.add_slide(slide_layout)

title_textbox = slide.shapes.add_textbox(Inches(5), Inches(0.4), Inches(6), Inches(0.6))
title = title_textbox.text_frame

pic = slide.shapes.add_picture('image_name.jpg', Inches(3.8), Inches(1.3), Inches(8.5), Inches(6))

subtitle_textbox = slide.shapes.add_textbox(Inches(5), Inches(7.5), Inches(6), Inches(1))
subtitle = subtitle_textbox.text_frame

I am using Python-pptx to create ppt, i want to fill the textbox background with other color.

from pptx import Presentation
from pptx.util import Inches, Pt
from datetime import datetime
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN

prs = Presentation()
prs.slide_width = Inches(16)
prs.slide_height = Inches(9)
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

slide_layout = prs.slide_layouts[5]
slide2 = prs.slides.add_slide(slide_layout)

title_textbox = slide.shapes.add_textbox(Inches(5), Inches(0.4), Inches(6), Inches(0.6))
title = title_textbox.text_frame

pic = slide.shapes.add_picture('image_name.jpg', Inches(3.8), Inches(1.3), Inches(8.5), Inches(6))

subtitle_textbox = slide.shapes.add_textbox(Inches(5), Inches(7.5), Inches(6), Inches(1))
subtitle = subtitle_textbox.text_frame

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很糊涂小朋友 2025-01-30 21:07:40

我不是100%确定我是否正确收到您的问题。您想创建一个文本框并上色吗?

我对它进行了一些更改:

from pptx import Presentation
from pptx.util import Inches
from pptx.dml.color import RGBColor

prs = Presentation()
prs.slide_width = Inches(16)
prs.slide_height = Inches(9)
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

textbox = slide.shapes.add_textbox(Inches(5), Inches(0.4), Inches(6), Inches(5))

textbox.text_frame.text = "foo"
textbox.fill.solid()
textbox.fill.fore_color.rgb = RGBColor(255, 0, 0)

这将创建一个带有文本“ foo”和“红色”的文本框。您可以找到更多可以操纵形状的选项在这里

I am not 100% sure if I got your question correctly. You want to create a textbox and color it?

I changed it a bit:

from pptx import Presentation
from pptx.util import Inches
from pptx.dml.color import RGBColor

prs = Presentation()
prs.slide_width = Inches(16)
prs.slide_height = Inches(9)
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

textbox = slide.shapes.add_textbox(Inches(5), Inches(0.4), Inches(6), Inches(5))

textbox.text_frame.text = "foo"
textbox.fill.solid()
textbox.fill.fore_color.rgb = RGBColor(255, 0, 0)

This creates a textbox with text "foo" and red color. You can find more options for the manipulation of your shape here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文