YashMK89 commited on
Commit
eab9d11
Β·
verified Β·
1 Parent(s): 6c28081

update code

Browse files
Files changed (1) hide show
  1. pages/1_πŸ“Š_Text_to_PPT.py +77 -75
pages/1_πŸ“Š_Text_to_PPT.py CHANGED
@@ -1334,26 +1334,25 @@ def create_question_slide(prs, question, question_num, theme):
1334
 
1335
  # Remove unused placeholders
1336
  for shape in slide.shapes:
1337
- if shape.has_text_frame and not shape.text.strip() and shape != getattr(slide, 'shapes', None):
1338
  sp = shape._element
1339
  sp.getparent().remove(sp)
1340
 
1341
  # Set title
1342
- title = slide.shapes.title if hasattr(slide, 'shapes') and hasattr(slide.shapes, 'title') else None
1343
  if title:
1344
  title.text = f"Question {question_num}"
1345
 
1346
  # Only apply custom formatting if not using a template
1347
- if "template_path" not in theme:
1348
- if title:
1349
- title.text_frame.paragraphs[0].font.color.rgb = theme["accent"]
1350
- title.text_frame.paragraphs[0].font.size = Pt(36)
1351
- title.text_frame.paragraphs[0].font.bold = True
1352
- if "title_font" in theme:
1353
- title.text_frame.paragraphs[0].font.name = theme["title_font"]
1354
-
1355
- # Add decorative line under title
1356
- add_title_separator(slide, title, theme["accent"])
1357
 
1358
  # Create content
1359
  left = Inches(1)
@@ -1412,26 +1411,25 @@ def create_answer_key_slide(prs, questions, theme):
1412
 
1413
  # Remove unused placeholders
1414
  for shape in slide.shapes:
1415
- if shape.has_text_frame and not shape.text.strip() and shape != getattr(slide, 'shapes', None):
1416
  sp = shape._element
1417
  sp.getparent().remove(sp)
1418
 
1419
  # Set title
1420
- title = slide.shapes.title if hasattr(slide, 'shapes') and hasattr(slide.shapes, 'title') else None
1421
  if title:
1422
  title.text = "Answer Key"
1423
 
1424
  # Only apply custom formatting if not using a template
1425
- if "template_path" not in theme:
1426
- if title:
1427
- title.text_frame.paragraphs[0].font.color.rgb = theme["accent"]
1428
- title.text_frame.paragraphs[0].font.size = Pt(36)
1429
- title.text_frame.paragraphs[0].font.bold = True
1430
- if "title_font" in theme:
1431
- title.text_frame.paragraphs[0].font.name = theme["title_font"]
1432
-
1433
- # Add decorative line under title
1434
- add_title_separator(slide, title, theme["accent"])
1435
 
1436
  # Create content
1437
  left = Inches(1.5)
@@ -1500,27 +1498,29 @@ def create_detailed_pptx(slides_data, questions, theme, branding_options=None, p
1500
 
1501
  # Remove unused placeholders
1502
  for shape in slide.shapes:
1503
- if shape.has_text_frame and not shape.text.strip() and shape != getattr(slide, 'shapes', None):
1504
  sp = shape._element
1505
  sp.getparent().remove(sp)
1506
 
 
 
 
1507
  # Set title if placeholder exists
1508
- if hasattr(slide, 'shapes') and hasattr(slide.shapes, 'title'):
1509
- title = slide.shapes.title
1510
- title.text = slide_info['title']
1511
- title.text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
1512
 
1513
  # Only apply custom formatting if not using a template
1514
  if "template_path" not in theme:
1515
  # Format title
1516
- title.text_frame.paragraphs[0].font.color.rgb = theme["title_color"]
1517
- title.text_frame.paragraphs[0].font.size = Pt(36)
1518
- title.text_frame.paragraphs[0].font.bold = True
1519
  if "title_font" in theme:
1520
- title.text_frame.paragraphs[0].font.name = theme["title_font"]
1521
 
1522
  # Add decorative line under title
1523
- add_title_separator(slide, title, theme["accent"])
1524
  else:
1525
  # Add title manually if no placeholder
1526
  left = Inches(1)
@@ -1536,7 +1536,8 @@ def create_detailed_pptx(slides_data, questions, theme, branding_options=None, p
1536
  p.font.size = Pt(36)
1537
  if "title_font" in theme:
1538
  p.font.name = theme["title_font"]
1539
- p.font.color.rgb = theme["title_color"]
 
1540
 
1541
  # Only apply custom formatting if not using a template
1542
  if "template_path" not in theme:
@@ -1616,14 +1617,14 @@ def create_detailed_pptx(slides_data, questions, theme, branding_options=None, p
1616
  except IndexError:
1617
  section_slide = prs.slides.add_slide(prs.slide_layouts[0]) # Fallback to title slide
1618
 
1619
- if hasattr(section_slide, 'shapes') and hasattr(section_slide.shapes, 'title'):
1620
- section_title = section_slide.shapes.title
1621
- section_title.text = "Knowledge Check"
1622
 
1623
  # Format section header
1624
  if "template_path" not in theme:
1625
- section_title.text_frame.paragraphs[0].font.color.rgb = theme["accent"]
1626
- section_title.text_frame.paragraphs[0].font.size = Pt(44)
1627
 
1628
  # Add each question on a separate slide
1629
  for i, question in enumerate(questions, 1):
@@ -1786,46 +1787,47 @@ def main():
1786
  st.markdown("4. Download your PowerPoint file")
1787
  st.markdown("5. Use 'Regenerate' buttons to improve specific slides")
1788
 
1789
- if st.button("Generate Presentation", type="primary", key="generate_btn"):
 
1790
  if not topic:
1791
  st.warning("Please enter a topic first!")
1792
  elif theme_option == "Example-Based Theme" and not uploaded_file:
1793
  st.warning("Please upload a PowerPoint template file first")
1794
  else:
1795
- progress_bar = st.progress(0)
1796
- status_text = st.empty()
1797
-
1798
- # Generate slide content
1799
- status_text.text("Generating content with AI...")
1800
- progress_bar.progress(20)
1801
- model = genai.GenerativeModel('gemini-2.0-flash')
1802
- slide_text = generate_slide_content(topic, slide_count, _model=model)
1803
-
1804
- # Parse slide content
1805
- status_text.text("Parsing slide content...")
1806
- progress_bar.progress(40)
1807
- slides_data, questionnaire, answer_key = parse_slide_content(slide_text)
1808
-
1809
- # Store in session state
1810
- st.session_state.slides_data = slides_data
1811
- st.session_state.questionnaire = questionnaire
1812
- st.session_state.answer_key = answer_key
1813
- st.session_state.regenerate_map = {i: False for i in range(len(slides_data))}
1814
-
1815
- # Create PowerPoint
1816
- status_text.text("Creating PowerPoint file...")
1817
- progress_bar.progress(70)
1818
- pptx_file = create_detailed_pptx(slides_data, questionnaire, theme, ppt_version=ppt_version)
1819
- st.session_state.pptx_file = pptx_file
1820
-
1821
- progress_bar.progress(100)
1822
- status_bar = st.empty()
1823
- status_bar.success("Done!")
1824
- time.sleep(1)
1825
- progress_bar.empty()
1826
- status_bar.empty()
1827
-
1828
- st.success("Presentation generated successfully!")
1829
 
1830
  # Show slide overview with regeneration options
1831
  if st.session_state.slides_data:
 
1334
 
1335
  # Remove unused placeholders
1336
  for shape in slide.shapes:
1337
+ if shape.has_text_frame and not shape.text.strip():
1338
  sp = shape._element
1339
  sp.getparent().remove(sp)
1340
 
1341
  # Set title
1342
+ title = slide.shapes.title if hasattr(slide.shapes, 'title') else None
1343
  if title:
1344
  title.text = f"Question {question_num}"
1345
 
1346
  # Only apply custom formatting if not using a template
1347
+ if "template_path" not in theme and title:
1348
+ title.text_frame.paragraphs[0].font.color.rgb = theme["accent"]
1349
+ title.text_frame.paragraphs[0].font.size = Pt(36)
1350
+ title.text_frame.paragraphs[0].font.bold = True
1351
+ if "title_font" in theme:
1352
+ title.text_frame.paragraphs[0].font.name = theme["title_font"]
1353
+
1354
+ # Add decorative line under title
1355
+ add_title_separator(slide, title, theme["accent"])
 
1356
 
1357
  # Create content
1358
  left = Inches(1)
 
1411
 
1412
  # Remove unused placeholders
1413
  for shape in slide.shapes:
1414
+ if shape.has_text_frame and not shape.text.strip():
1415
  sp = shape._element
1416
  sp.getparent().remove(sp)
1417
 
1418
  # Set title
1419
+ title = slide.shapes.title if hasattr(slide.shapes, 'title') else None
1420
  if title:
1421
  title.text = "Answer Key"
1422
 
1423
  # Only apply custom formatting if not using a template
1424
+ if "template_path" not in theme and title:
1425
+ title.text_frame.paragraphs[0].font.color.rgb = theme["accent"]
1426
+ title.text_frame.paragraphs[0].font.size = Pt(36)
1427
+ title.text_frame.paragraphs[0].font.bold = True
1428
+ if "title_font" in theme:
1429
+ title.text_frame.paragraphs[0].font.name = theme["title_font"]
1430
+
1431
+ # Add decorative line under title
1432
+ add_title_separator(slide, title, theme["accent"])
 
1433
 
1434
  # Create content
1435
  left = Inches(1.5)
 
1498
 
1499
  # Remove unused placeholders
1500
  for shape in slide.shapes:
1501
+ if shape.has_text_frame and not shape.text.strip():
1502
  sp = shape._element
1503
  sp.getparent().remove(sp)
1504
 
1505
+ # Try to get the title placeholder
1506
+ title_placeholder = slide.shapes.title if hasattr(slide.shapes, 'title') else None
1507
+
1508
  # Set title if placeholder exists
1509
+ if title_placeholder is not None:
1510
+ title_placeholder.text = slide_info['title']
1511
+ title_placeholder.text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
 
1512
 
1513
  # Only apply custom formatting if not using a template
1514
  if "template_path" not in theme:
1515
  # Format title
1516
+ title_placeholder.text_frame.paragraphs[0].font.color.rgb = theme["title_color"]
1517
+ title_placeholder.text_frame.paragraphs[0].font.size = Pt(36)
1518
+ title_placeholder.text_frame.paragraphs[0].font.bold = True
1519
  if "title_font" in theme:
1520
+ title_placeholder.text_frame.paragraphs[0].font.name = theme["title_font"]
1521
 
1522
  # Add decorative line under title
1523
+ add_title_separator(slide, title_placeholder, theme["accent"])
1524
  else:
1525
  # Add title manually if no placeholder
1526
  left = Inches(1)
 
1536
  p.font.size = Pt(36)
1537
  if "title_font" in theme:
1538
  p.font.name = theme["title_font"]
1539
+ if "title_color" in theme:
1540
+ p.font.color.rgb = theme["title_color"]
1541
 
1542
  # Only apply custom formatting if not using a template
1543
  if "template_path" not in theme:
 
1617
  except IndexError:
1618
  section_slide = prs.slides.add_slide(prs.slide_layouts[0]) # Fallback to title slide
1619
 
1620
+ title_placeholder = section_slide.shapes.title if hasattr(section_slide.shapes, 'title') else None
1621
+ if title_placeholder is not None:
1622
+ title_placeholder.text = "Knowledge Check"
1623
 
1624
  # Format section header
1625
  if "template_path" not in theme:
1626
+ title_placeholder.text_frame.paragraphs[0].font.color.rgb = theme["accent"]
1627
+ title_placeholder.text_frame.paragraphs[0].font.size = Pt(44)
1628
 
1629
  # Add each question on a separate slide
1630
  for i, question in enumerate(questions, 1):
 
1787
  st.markdown("4. Download your PowerPoint file")
1788
  st.markdown("5. Use 'Regenerate' buttons to improve specific slides")
1789
 
1790
+ generate_btn = st.button("Generate Presentation", type="primary", key="generate_btn")
1791
+ if generate_btn:
1792
  if not topic:
1793
  st.warning("Please enter a topic first!")
1794
  elif theme_option == "Example-Based Theme" and not uploaded_file:
1795
  st.warning("Please upload a PowerPoint template file first")
1796
  else:
1797
+ with st.spinner("Generating your presentation..."):
1798
+ progress_bar = st.progress(0)
1799
+ status_text = st.empty()
1800
+
1801
+ # Generate slide content
1802
+ status_text.text("Generating content with AI...")
1803
+ progress_bar.progress(20)
1804
+ model = genai.GenerativeModel('gemini-2.0-flash')
1805
+ slide_text = generate_slide_content(topic, slide_count, _model=model)
1806
+
1807
+ # Parse slide content
1808
+ status_text.text("Parsing slide content...")
1809
+ progress_bar.progress(40)
1810
+ slides_data, questionnaire, answer_key = parse_slide_content(slide_text)
1811
+
1812
+ # Store in session state
1813
+ st.session_state.slides_data = slides_data
1814
+ st.session_state.questionnaire = questionnaire
1815
+ st.session_state.answer_key = answer_key
1816
+ st.session_state.regenerate_map = {i: False for i in range(len(slides_data))}
1817
+
1818
+ # Create PowerPoint
1819
+ status_text.text("Creating PowerPoint file...")
1820
+ progress_bar.progress(70)
1821
+ pptx_file = create_detailed_pptx(slides_data, questionnaire, theme, ppt_version=ppt_version)
1822
+ st.session_state.pptx_file = pptx_file
1823
+
1824
+ progress_bar.progress(100)
1825
+ status_text.text("Done!")
1826
+ time.sleep(1)
1827
+ progress_bar.empty()
1828
+ status_text.empty()
1829
+
1830
+ st.success("Presentation generated successfully!")
1831
 
1832
  # Show slide overview with regeneration options
1833
  if st.session_state.slides_data: