Created
September 28, 2023 14:49
-
-
Save kepler471/aaaaa0537efab87ff375b0925cc898af to your computer and use it in GitHub Desktop.
python-pptx example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GPT-3.5 generated example slide using python-pptx | |
from pptx import Presentation | |
from pptx.util import Inches | |
from pptx.enum.shapes import MSO_SHAPE | |
from pptx.dml.color import RGBColor | |
# Create a PowerPoint presentation | |
prs = Presentation() | |
# Add a slide with a title and content layout (Title and Content slide) | |
slide_layout = prs.slide_layouts[1] # 1 corresponds to Title and Content layout | |
slide = prs.slides.add_slide(slide_layout) | |
# Add a title to the slide | |
title = slide.shapes.title | |
title.text = "Sample Slide Title" | |
# Add a content placeholder for the paragraph | |
left = Inches(1) | |
top = Inches(1.5) | |
width = Inches(8) | |
height = Inches(3.5) | |
txBox = slide.shapes.add_textbox(left, top, width, height) | |
tf = txBox.text_frame | |
# Add a paragraph of example text | |
p = tf.add_paragraph() | |
p.text = "This is an example paragraph of text in PowerPoint." | |
p.space_after = Inches(0.1) | |
# Add URL links | |
urls = [ | |
{"text": "OpenAI", "url": "https://www.openai.com"}, | |
{"text": "Python", "url": "https://www.python.org"}, | |
{"text": "GitHub", "url": "https://www.github.com"}, | |
] | |
for link in urls: | |
p = tf.add_paragraph() | |
p.space_after = Inches(0.1) | |
# Add a hyperlink to the text | |
run = p.add_run() | |
run.text = link["text"] | |
hyperlink = run.hyperlink | |
hyperlink.address = link["url"] | |
run.font.color.rgb = RGBColor(0, 102, 204) # Set text color to blue | |
# Save the presentation to a file | |
prs.save("sample_presentation.pptx") | |
print("PowerPoint slide created successfully.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment