Spaces:
Sleeping
Sleeping
Update app.py
Browse filesStreamlit app script with new features:
Custom Slide Layouts
Image Insertion
Custom Styling Options
Theme Selection
app.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from pptx import Presentation
|
|
|
|
|
|
|
| 3 |
from collections import OrderedDict
|
|
|
|
| 4 |
|
| 5 |
# Define templates for different presentation types
|
| 6 |
templates = {
|
|
@@ -45,10 +48,20 @@ templates = {
|
|
| 45 |
]
|
| 46 |
}
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# Function to create PowerPoint presentation based on user inputs
|
| 49 |
-
def create_presentation(title_slide_info, sections):
|
| 50 |
prs = Presentation()
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
# Add a title slide
|
| 53 |
title_slide_layout = prs.slide_layouts[0]
|
| 54 |
title_slide = prs.slides.add_slide(title_slide_layout)
|
|
@@ -65,16 +78,40 @@ def create_presentation(title_slide_info, sections):
|
|
| 65 |
title_placeholder = slide.shapes.title
|
| 66 |
body_placeholder = slide.placeholders[1]
|
| 67 |
title_placeholder.text = f"{section_title} - {subsection_title}"
|
| 68 |
-
|
|
|
|
| 69 |
for bullet in bullet_points:
|
| 70 |
-
p =
|
| 71 |
p.text = bullet
|
|
|
|
| 72 |
|
| 73 |
# Save the presentation
|
| 74 |
filename = 'custom_presentation.pptx'
|
| 75 |
prs.save(filename)
|
| 76 |
return filename
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# Function to handle dynamic section addition
|
| 79 |
def add_new_section(section_counter):
|
| 80 |
new_section_title = f"Section {section_counter}"
|
|
@@ -108,6 +145,9 @@ with st.sidebar.expander("Title Slide Options", expanded=False):
|
|
| 108 |
'date': st.text_input("Date", "Presentation Date")
|
| 109 |
}
|
| 110 |
|
|
|
|
|
|
|
|
|
|
| 111 |
# Option to add more sections dynamically
|
| 112 |
if st.sidebar.button("Add More Section"):
|
| 113 |
add_new_section(st.session_state.section_counter)
|
|
@@ -134,9 +174,26 @@ for section_title, subsections in list(st.session_state.sections.items()):
|
|
| 134 |
if st.button(f"Add Subsection to {section_title}", key=f"add_subsection_{section_title}"):
|
| 135 |
add_new_subsection(section_title)
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
# Generate and download presentation
|
| 138 |
if st.button('Generate Presentation'):
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
with open(file_path, "rb") as file:
|
| 141 |
st.download_button(
|
| 142 |
label="Download Presentation",
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from pptx import Presentation
|
| 3 |
+
from pptx.util import Inches, Pt
|
| 4 |
+
from pptx.dml.color import RGBColor
|
| 5 |
from collections import OrderedDict
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
# Define templates for different presentation types
|
| 9 |
templates = {
|
|
|
|
| 48 |
]
|
| 49 |
}
|
| 50 |
|
| 51 |
+
# Define themes
|
| 52 |
+
themes = {
|
| 53 |
+
"Corporate": {"font": "Arial", "font_size": 20, "color": "0000FF"},
|
| 54 |
+
"Casual": {"font": "Comic Sans MS", "font_size": 18, "color": "FF5733"},
|
| 55 |
+
# Add more themes as needed
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
# Function to create PowerPoint presentation based on user inputs
|
| 59 |
+
def create_presentation(title_slide_info, sections, theme_name):
|
| 60 |
prs = Presentation()
|
| 61 |
|
| 62 |
+
# Apply theme
|
| 63 |
+
theme = themes[theme_name]
|
| 64 |
+
|
| 65 |
# Add a title slide
|
| 66 |
title_slide_layout = prs.slide_layouts[0]
|
| 67 |
title_slide = prs.slides.add_slide(title_slide_layout)
|
|
|
|
| 78 |
title_placeholder = slide.shapes.title
|
| 79 |
body_placeholder = slide.placeholders[1]
|
| 80 |
title_placeholder.text = f"{section_title} - {subsection_title}"
|
| 81 |
+
|
| 82 |
+
# Custom styling options
|
| 83 |
for bullet in bullet_points:
|
| 84 |
+
p = body_placeholder.text_frame.add_paragraph()
|
| 85 |
p.text = bullet
|
| 86 |
+
style_text(p, theme["font_size"], theme["color"])
|
| 87 |
|
| 88 |
# Save the presentation
|
| 89 |
filename = 'custom_presentation.pptx'
|
| 90 |
prs.save(filename)
|
| 91 |
return filename
|
| 92 |
|
| 93 |
+
# Function to style text
|
| 94 |
+
def style_text(paragraph, font_size, font_color):
|
| 95 |
+
run = paragraph.add_run()
|
| 96 |
+
font = run.font
|
| 97 |
+
font.size = Pt(font_size)
|
| 98 |
+
font.color.rgb = RGBColor.from_string(font_color)
|
| 99 |
+
|
| 100 |
+
# Function to add slide with custom layout
|
| 101 |
+
def add_slide_with_layout(prs, layout_index, title_text, content_points):
|
| 102 |
+
slide_layout = prs.slide_layouts[layout_index]
|
| 103 |
+
slide = prs.slides.add_slide(slide_layout)
|
| 104 |
+
title_placeholder = slide.shapes.title
|
| 105 |
+
body_placeholder = slide.placeholders[1]
|
| 106 |
+
title_placeholder.text = title_text
|
| 107 |
+
for point in content_points:
|
| 108 |
+
p = body_placeholder.text_frame.add_paragraph()
|
| 109 |
+
p.text = point
|
| 110 |
+
|
| 111 |
+
# Function to add image to a slide
|
| 112 |
+
def add_image_to_slide(slide, image_path):
|
| 113 |
+
slide.shapes.add_picture(image_path, Inches(1), Inches(1), width=Inches(5), height=Inches(3))
|
| 114 |
+
|
| 115 |
# Function to handle dynamic section addition
|
| 116 |
def add_new_section(section_counter):
|
| 117 |
new_section_title = f"Section {section_counter}"
|
|
|
|
| 145 |
'date': st.text_input("Date", "Presentation Date")
|
| 146 |
}
|
| 147 |
|
| 148 |
+
# Theme selection
|
| 149 |
+
theme_name = st.sidebar.selectbox("Select Theme", list(themes.keys()))
|
| 150 |
+
|
| 151 |
# Option to add more sections dynamically
|
| 152 |
if st.sidebar.button("Add More Section"):
|
| 153 |
add_new_section(st.session_state.section_counter)
|
|
|
|
| 174 |
if st.button(f"Add Subsection to {section_title}", key=f"add_subsection_{section_title}"):
|
| 175 |
add_new_subsection(section_title)
|
| 176 |
|
| 177 |
+
# Image upload for each section
|
| 178 |
+
uploaded_images = {}
|
| 179 |
+
for section_title in st.session_state.sections:
|
| 180 |
+
uploaded_image = st.file_uploader(f"Upload image for {section_title}", type=["png", "jpg", "jpeg"], key=f"image_{section_title}")
|
| 181 |
+
if uploaded_image is not None:
|
| 182 |
+
uploaded_images[section_title] = uploaded_image
|
| 183 |
+
|
| 184 |
# Generate and download presentation
|
| 185 |
if st.button('Generate Presentation'):
|
| 186 |
+
prs = Presentation()
|
| 187 |
+
for section_title, subsections in st.session_state.sections.items():
|
| 188 |
+
for subsection_title, bullet_points in subsections.items():
|
| 189 |
+
slide = add_slide_with_layout(prs, 1, f"{section_title} - {subsection_title}", bullet_points)
|
| 190 |
+
if section_title in uploaded_images:
|
| 191 |
+
image_path = os.path.join(os.getcwd(), uploaded_images[section_title].name)
|
| 192 |
+
with open(image_path, "wb") as f:
|
| 193 |
+
f.write(uploaded_images[section_title].getbuffer())
|
| 194 |
+
add_image_to_slide(slide, image_path)
|
| 195 |
+
|
| 196 |
+
file_path = create_presentation(title_slide_info, st.session_state.sections, theme_name)
|
| 197 |
with open(file_path, "rb") as file:
|
| 198 |
st.download_button(
|
| 199 |
label="Download Presentation",
|