简体   繁体   English

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

[英]How to fill the textbox background color using Python-pptx?

I am using Python-pptx to create ppt, i want to fill the textbox background with other color.我正在使用 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 not 100% sure if I got your question correctly.我不是 100% 确定我是否正确回答了你的问题。 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.这将创建一个带有文本“foo”和红色的文本框。 You can find more options for the manipulation of your shape here .您可以在此处找到更多操作形状的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM