python pptx表格里的字体怎么调整_Python pptx-自选图形上的文本参数(字体、大小、位置)...

本文介绍如何使用python-pptx库调整PPT中形状文本的字体、大小和位置。通过添加形状,设置填充和线条颜色,然后通过调整文本框的位置和大小,最后设置文本框内文字的字体、大小、颜色和样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何设置形状的字体和文本大小?

一个对象代替两个独立的对象(形状和文本,如何对形状使用run)

只是不知道如何设置自选图形对象的Textframe实例的参数。from pptx import Presentation

from pptx.enum.shapes import MSO_SHAPE

from pptx.enum.dml import MSO_THEME_COLOR

from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE

from pptx.util import Inches, Pt

prs = Presentation('Input.pptx')

slide_layout = prs.slide_layouts[2]

slide = prs.slides.add_slide(slide_layout)

shape = slide.shapes

#Title

shape.title.text = "Title of the slide"

# Shape position

left = Inches(0.5)

top = Inches(1.5)

width = Inches(2.0)

height = Inches(0.2)

box = shape.add_shape(MSO_SHAPE.RECTANGLE, left, top, width, height)

#Fill

fill = box.fill

line = box.line

fill.solid()

fill.fore_color.theme_color = MSO_THEME_COLOR.ACCENT_2

line.color.theme_color = MSO_THEME_COLOR.ACCENT_2

# How can I set font, size of text for the shape ?

# One Object for instead two seperate ones

#box_text.font.bold = True

# Text position

t_left = Inches(0.5)

t_top = Inches(1.4)

t_width = Inches(2.0)

t_height = Inches(0.4)

#Text

txBox = slide.shapes.add_textbox(t_left, t_top, t_width,t_height)

tf = txBox.text_frame.paragraphs[0]

tf.vertical_anchor = MSO_ANCHOR.TOP

tf.word_wrap = False

tf.margin_top = 0

tf.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT

run = tf.add_run()

run.text = "Text on the Shape"

font = run.font

font.name = 'Calibri'

font.size = Pt(18)

font.bold = True

font.italic = None # cause value to be inherited from theme

font.color.theme_color = MSO_THEME_COLOR.ACCENT_5

prs.save('Out.pptx')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值