Add docs/tech_docs/python/python-pptx.md
This commit is contained in:
107
docs/tech_docs/python/python-pptx.md
Normal file
107
docs/tech_docs/python/python-pptx.md
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
For working with PowerPoint presentations in Python, the `python-pptx` library is a go-to solution. It allows for creating new presentations or modifying existing ones, adding slides, manipulating text, inserting images, and more. Here’s a concise reference guide covering common use cases with `python-pptx`, formatted in Markdown syntax:
|
||||||
|
|
||||||
|
# `python-pptx` Reference Guide
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
```
|
||||||
|
pip install python-pptx
|
||||||
|
```
|
||||||
|
|
||||||
|
## Creating a New Presentation
|
||||||
|
```python
|
||||||
|
from pptx import Presentation
|
||||||
|
|
||||||
|
# Create a new Presentation object
|
||||||
|
prs = Presentation()
|
||||||
|
|
||||||
|
# Save the presentation
|
||||||
|
prs.save('new_presentation.pptx')
|
||||||
|
```
|
||||||
|
|
||||||
|
## Opening Existing Presentations
|
||||||
|
```python
|
||||||
|
# Load an existing presentation
|
||||||
|
prs = Presentation('path_to_existing_file.pptx')
|
||||||
|
```
|
||||||
|
|
||||||
|
## Adding Slides
|
||||||
|
```python
|
||||||
|
from pptx.util import Inches
|
||||||
|
|
||||||
|
# Add a slide with a title and content layout
|
||||||
|
slide_layout = prs.slide_layouts[1] # 0 is the layout for a title slide, 1 for title and content
|
||||||
|
slide = prs.slides.add_slide(slide_layout)
|
||||||
|
|
||||||
|
# Set title and content
|
||||||
|
title = slide.shapes.title
|
||||||
|
title.text = "This is the slide title"
|
||||||
|
|
||||||
|
content = slide.placeholders[1]
|
||||||
|
content.text = "This is the slide content"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Adding Text to Slides
|
||||||
|
```python
|
||||||
|
# Add a textbox to a slide
|
||||||
|
x, y, cx, cy = Inches(2), Inches(2), Inches(4), Inches(1.5)
|
||||||
|
textbox = slide.shapes.add_textbox(x, y, cx, cy)
|
||||||
|
|
||||||
|
# Add text to the textbox
|
||||||
|
text_frame = textbox.text_frame
|
||||||
|
p = text_frame.add_paragraph()
|
||||||
|
p.text = "This is a textbox"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Inserting Images
|
||||||
|
```python
|
||||||
|
# Add an image to a slide
|
||||||
|
img_path = 'path_to_image.jpg'
|
||||||
|
x, y = Inches(1), Inches(1)
|
||||||
|
slide.shapes.add_picture(img_path, x, y, width=Inches(4))
|
||||||
|
```
|
||||||
|
|
||||||
|
## Working with Shapes
|
||||||
|
```python
|
||||||
|
# Add a shape (e.g., rectangle)
|
||||||
|
x, y, cx, cy = Inches(3), Inches(3), Inches(2), Inches(1)
|
||||||
|
shape = slide.shapes.add_shape(
|
||||||
|
MSO_SHAPE.RECTANGLE, x, y, cx, cy
|
||||||
|
)
|
||||||
|
shape.fill.solid()
|
||||||
|
shape.fill.fore_color.rgb = RGBColor(255, 0, 0) # Red fill
|
||||||
|
shape.text = "A rectangle"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Applying Styles to Text
|
||||||
|
```python
|
||||||
|
from pptx.dml.color import RGBColor
|
||||||
|
from pptx.util import Pt
|
||||||
|
|
||||||
|
# Set font size, bold, italic, and color
|
||||||
|
run = p.add_run()
|
||||||
|
run.text = "Stylized text"
|
||||||
|
font = run.font
|
||||||
|
font.size = Pt(20)
|
||||||
|
font.bold = True
|
||||||
|
font.italic = True
|
||||||
|
font.color.rgb = RGBColor(0, 102, 204) # Blue
|
||||||
|
```
|
||||||
|
|
||||||
|
## Managing Slide Layouts
|
||||||
|
```python
|
||||||
|
# Iterate through available slide layouts
|
||||||
|
for layout in prs.slide_layouts:
|
||||||
|
print(layout.name)
|
||||||
|
|
||||||
|
# Use a specific layout for a new slide
|
||||||
|
custom_layout = prs.slide_layouts[2] # Choosing a specific layout
|
||||||
|
slide = prs.slides.add_slide(custom_layout)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Saving Presentations
|
||||||
|
```python
|
||||||
|
# Save changes to the presentation
|
||||||
|
prs.save('modified_presentation.pptx')
|
||||||
|
```
|
||||||
|
|
||||||
|
This guide covers foundational operations like creating and saving presentations, adding slides with various layouts, inserting and formatting text, adding images, and working with shapes. The `python-pptx` library offers extensive functionalities for PowerPoint manipulation, making it a powerful tool for automating presentation creation and editing tasks in Python.
|
||||||
Reference in New Issue
Block a user