简体   繁体   English

如何在python pptx中更改文本框中文本的字体大小

[英]How to change the font size of the text in textbox in python pptx

I want to change the font size of the test in textbox I tried like...........我想在我尝试过的文本框中更改测试的字体大小.......

import os

from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
shapes = slide.shapes
left = top = width = height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
tf.text = "Hello World"


prs.save('test.pptx')
os.startfile('test.pptx')

It is creating the ppt with Hello world text but I am not able to change the font size.它正在创建带有 Hello world 文本的 ppt,但我无法更改字体大小。 Second way I tried is like..我尝试的第二种方式就像..

import os

from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
shapes = slide.shapes
left = top = width = height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame


p = tf.add_paragraph()
p.text = "Hello World"

p.font.size = Pt(40)
prs.save('test.pptx')
os.startfile('test.pptx')

But in this extra line is created at starting of text box and I don't want that line.但是在这个额外的行中是在文本框的开头创建的,我不想要那行。

So is there any solution using first approach to change the font size or using the second approach not to have extra line?那么有没有使用第一种方法来改变字体大小或使用第二种方法没有多余线条的解决方案?

Try this.尝试这个。 Here you can find more info about text formating in pptx-python https://python-pptx.readthedocs.io/en/latest/user/text.html在这里您可以找到有关 pptx-python 中文本格式的更多信息https://python-pptx.readthedocs.io/en/latest/user/text.html

import os

from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
shapes = slide.shapes
left = top = width = height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame

p = tf.paragraphs[0]
run = p.add_run()
run.text = "Hello World"

font = run.font
font.name = 'Calibri'
font.size = Pt(64)

prs.save('test.pptx')
os.startfile('test.pptx')

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

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