I have created a simple python app that converts each line of a text file into the title of each slide in a pptx file, using the python-pptx module and Bing AI. The app works fine, except for one issue: the font size of the title is too large and I cannot change it through the code.
I have tried different ways to adjust the font size, such as using the font.size attribute of the text_frame.paragraphs[0].font object, as suggested in this answer, but none of them worked. I have also read the python-pptx documentation but I could not find a solution. I kept getting similar problem like this.
line 31, in create_presentation title_shape.font.size = Pt(32) ^^^^^^^^^^^^^^^^ AttributeError: 'SlidePlaceholder' object has no attribute 'font'
How can I change the font size of the slide title in python-pptx? Any help would be appreciated. Thank you.
import tkinter as tk
from tkinter import filedialog
from pptx import Presentation
from pptx.util import Pt
import os # Import the os module
def create_presentation():
# Open a file dialog to select a text file
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
# Read the text file and get the slide titles
with open(file_path) as f:
slide_titles = f.read().splitlines()
# Create a new PowerPoint presentation
prs = Presentation()
# Use the title and content slide layout (index 1)
title_and_content_layout = prs.slide_layouts[1]
# Add a slide for each title in the list
for title in slide_titles:
# Remove the leading hyphen or dash from the title
title = title.lstrip('- ')
slide = prs.slides.add_slide(title_and_content_layout)
title_shape = slide.shapes.title
title_shape.text = title
title_shape.font.size = Pt(32)
# Change the font size in title to 32 pt
# You can add some content text here, or leave it blank
# content_shape = slide.placeholders[1]
# content_shape.text = "say something here"
# Get the directory of the input file
dir_path = os.path.dirname(file_path)
# Extract the filename from the file path
file_name = os.path.basename(file_path)
# Split the file name into base and extension
base, ext = os.path.splitext(file_name)
# Replace the extension with .pptx
new_file_name = base + ".pptx"
# Join the directory and the new file name
output_path = os.path.join(dir_path, new_file_name)
# Save the PowerPoint presentation
prs.save(output_path)
root.destroy()
create_presentation()
A
textframecontainsrunobjects. Apply thefont.sizechange to that.