简体   繁体   中英

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. 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

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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