#!/usr/bin/env python3 # https://huggingface.co/spaces/MisterAI/GenDoc_05 # /home/user/python_pptx/python_pptx.py_02 from pptx import Presentation from pptx.util import Inches, Pt from pptx.enum.text import PP_ALIGN class PresentationGenerator: def __init__(self): self.theme_colors = { "title": "1F4E79", # Bleu foncé "subtitle": "277BC0", # Bleu clair "text": "2A2A2A", # Gris foncé "background": "FFFFFF", # Blanc "accent": "98C1D9" # Bleu pastel } self.default_font = "Calibri" def parse_presentation_content(self, content): slides = [] current_slide = None lines = content.split('\n') for line in lines: line = line.strip() if line == "#Debut Slide Titre#": current_slide = {'type': 'title', 'title': ''} elif line == "#Debut Slide Ressources Utiles#": if current_slide: slides.append(current_slide) current_slide = {'type': 'resources', 'title': 'Ressources Utiles', 'points': []} elif line.startswith("#Debut Slide"): if current_slide: slides.append(current_slide) current_slide = {'type': 'content', 'title': '', 'points': []} elif line.startswith("## Titre:"): if current_slide: current_slide['title'] = line[8:].strip() elif line.startswith("### Points:"): if current_slide: current_slide['points'] = [] elif line.startswith("- ") and current_slide: if current_slide: current_slide['points'].append(line[2:].strip()) elif line == "#Fin Slide Ressources Utiles#" or line.startswith("#Fin Slide"): if current_slide: slides.append(current_slide) current_slide = None return slides def create_presentation(self, slides): prs = Presentation() # Determine the maximum content slide for font size adjustment max_content_length = max(len(" ".join(slide['points'])) for slide in slides if slide['type'] == 'content') # Set font sizes based on the maximum content length base_font_size = 32 title_font_size = 44 if max_content_length > 300: # Example threshold base_font_size = 24 title_font_size = 36 # Title Slide title_slide = prs.slides.add_slide(prs.slide_layouts[0]) title_slide.shapes.title.text = slides[0]['title'] title_slide.shapes.title.text_frame.paragraphs[0].font.size = Pt(title_font_size) title_slide.shapes.title.text_frame.paragraphs[0].font.name = self.default_font title_slide.shapes.title.text_frame.paragraphs[0].font.color.rgb = self.rgb_to_tuple(self.theme_colors["title"]) for slide in slides[1:]: content_slide = prs.slides.add_slide(prs.slide_layouts[1]) content_slide.shapes.title.text = slide['title'] content_slide.shapes.title.text_frame.paragraphs[0].font.size = Pt(title_font_size) content_slide.shapes.title.text_frame.paragraphs[0].font.name = self.default_font content_slide.shapes.title.text_frame.paragraphs[0].font.color.rgb = self.rgb_to_tuple(self.theme_colors["subtitle"]) if slide['points']: body = content_slide.shapes.placeholders[1].text_frame body.clear() for point in slide['points']: p = body.add_paragraph() p.text = point p.font.size = Pt(base_font_size) p.font.name = self.default_font p.font.color.rgb = self.rgb_to_tuple(self.theme_colors["text"]) return prs def rgb_to_tuple(self, hex_color): # Convert hex color to RGB tuple hex_color = hex_color.lstrip('#') return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))