Skip to content

Commit

Permalink
update ppt template
Browse files Browse the repository at this point in the history
  • Loading branch information
crvernon committed Sep 25, 2024
1 parent 7e4246a commit 5869e17
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from docxtpl import DocxTemplate
from pptx import Presentation
from pptx.util import Pt
from pptx.enum.text import PP_ALIGN
from openai import OpenAI
import streamlit as st

Expand Down Expand Up @@ -681,6 +682,7 @@
# build container content
if objective_container.button('Generate Objective'):
st.session_state.objective_response = hlt.generate_content(
client=st.session_state.client,
container=objective_container,
content=content_dict["content"],
prompt_name="objective",
Expand Down Expand Up @@ -849,3 +851,109 @@
label_visibility="collapsed",
height=250
)


# ppt_template_file = importlib.resources.files('highlight.data').joinpath('highlight_template.pptx')


# Add PowerPoint export container at the end
export_ppt_container = st.container()
export_ppt_container.markdown("##### Export PowerPoint Presentation with New Content")

if ("title_response" in st.session_state and
"objective_response" in st.session_state and
"ppt_impact_response" in st.session_state and
"approach_response" in st.session_state):

if export_ppt_container.button('Export PowerPoint'):

try:
# Load the PowerPoint template
ppt_template_file = importlib.resources.files('highlight.data').joinpath('highlight_template.pptx')
prs = Presentation(ppt_template_file)

# Split the impact and approach responses into bullet points (assuming they are separated by newlines)
impact_points = st.session_state.ppt_impact_response.split("\n")
approach_points = st.session_state.approach_response.split("\n")

# Iterate over all slides to find the text boxes labeled "impact_0", "impact_1", "impact_2"
for slide in prs.slides:
for shape in slide.shapes:
if shape.has_text_frame:
# Handle title insertion and maintain font size and bold
if "title" in shape.text_frame.text:
shape.text_frame.text = st.session_state.title_response

# Ensure font size and bold settings are maintained for each paragraph
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(24) # Example size, adjust as needed
run.font.bold = True # Maintain bold
run.alignment = PP_ALIGN.LEFT # Align title

# Handle citation insertion and maintain font size and bold
if "citation" in shape.text_frame.text:
shape.text_frame.text = st.session_state.citation

# Ensure font size and bold settings are maintained for each paragraph
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(12) # Example size for citation, adjust as needed
run.font.bold = False # Citation typically isn't bold
run.alignment = PP_ALIGN.LEFT # Align citation

if shape.text_frame.text == "objective_0":
# Set the text of the text box to the objective response
shape.text_frame.text = st.session_state.objective_response

# Optional: Adjust font size and alignment for the objective
for paragraph in shape.text_frame.paragraphs:
paragraph.font.size = Pt(14) # Set font size
paragraph.alignment = PP_ALIGN.LEFT # Set alignment

# Handle approach bullet points
if "approach_0" in shape.text_frame.text:
# Clear existing paragraphs in the text frame
shape.text_frame.clear()

# Add bullet points for approach
for i, approach_point in enumerate(approach_points[:3]): # Only take the first 3 approach points
p = shape.text_frame.add_paragraph()
p.text = approach_point
p.level = 0 # This sets it as a bullet point
p.font.size = Pt(14) # Adjust bullet point font size
p.alignment = PP_ALIGN.LEFT # Align bullet points

# Handle the impact bullet points in the same text box
if "impact_0" in shape.text_frame.text:
# Clear the existing paragraphs
shape.text_frame.clear()

# Add bullet points for impact
for i, impact_point in enumerate(impact_points[:3]): # Only take the first 3 impact points
p = shape.text_frame.add_paragraph()
p.text = impact_point
p.level = 0 # This sets it as a bullet point
p.font.size = Pt(14) # Adjust bullet point font size
p.alignment = PP_ALIGN.LEFT # Align bullet points

# Save the modified presentation to a BytesIO object
ppt_io = io.BytesIO()
prs.save(ppt_io)
ppt_io.seek(0)

# Provide a download button for the user
export_ppt_container.download_button(
label="Export PowerPoint Presentation",
data=ppt_io,
file_name="modified_highlight_template.pptx",
mime="application/vnd.openxmlformats-officedocument.presentationml.presentation"
)

export_ppt_container.success("PowerPoint presentation generated successfully!", icon="✅")

except Exception as e:
export_ppt_container.error(f"An error occurred while generating the PowerPoint: {e}", icon="🚨")

else:
export_ppt_container.error("Please generate the objective and impact responses before exporting.", icon="⚠️")
Binary file modified highlight/data/highlight_template.docx
Binary file not shown.
Binary file modified highlight/data/highlight_template.pptx
Binary file not shown.

0 comments on commit 5869e17

Please sign in to comment.