import gradio as gr import re from pptx import Presentation from pptx.util import Pt from pptx.dml.color import RGBColor from pptx.enum.shapes import PP_PLACEHOLDER from pptx.enum.dml import MSO_FILL from pptx.enum.text import PP_ALIGN import os import tempfile import io # --- 1. CONFIGURATION AND CONSTANTS --- # List of common, web-safe fonts for the dropdown menu COMMON_FONTS = [ 'Arial', 'Arial Black', 'Calibri', 'Calibri Light', 'Cambria', 'Candara', 'Century Gothic', 'Consolas', 'Constantia', 'Corbel', 'Courier New', 'Franklin Gothic Medium', 'Gabriola', 'Gadugi', 'Georgia', 'Gill Sans MT', 'Impact', 'Lucida Console', 'Lucida Sans Unicode', 'Palatino Linotype', 'Rockwell', 'Segoe UI', 'Sitka', 'Tahoma', 'Times New Roman', 'Trebuchet MS', 'Verdana' ] # Default styles that will populate the UI DEFAULT_STYLES = { 'title': {'font_name': 'Calibri', 'font_size': 44, 'bold': True, 'color': '#000000'}, 'subtitle': {'font_name': 'Calibri', 'font_size': 24, 'bold': False, 'color': '#333333'}, 'body_title': {'font_name': 'Calibri', 'font_size': 36, 'bold': True, 'color': '#000000'}, 'body_level_0': {'font_name': 'Calibri', 'font_size': 24, 'bold': False, 'color': '#1E1E1E'}, 'body_level_1': {'font_name': 'Calibri', 'font_size': 20, 'bold': False, 'color': '#1E1E1E'}, 'body_level_2': {'font_name': 'Calibri', 'font_size': 18, 'bold': False, 'color': '#1E1E1E'}, 'hyperlink': {'font_name': 'Calibri', 'font_size': 16, 'underline': True, 'color': '#0563C1'} } # Mapping for bullet points to indentation levels BULLET_MAP = {'•': 0, '◦': 1, '▪': 2} HYPERLINK_RE = re.compile(r'^(.*?):\s*(https?://\S+)$') # --- 2. DEFAULT CONTENT AND EXAMPLES --- DEFAULT_TEMPLATE = """ Slide 1: Title Slide • Python-Powered Presentation Architect • A Gradio & Python-pptx Project Slide 2: Introduction • Problem: Creating presentations is time-consuming. • Solution: Automate slide generation from simple text outlines. • Technology: ◦ Python for backend logic. ◦ `python-pptx` for presentation manipulation. ◦ Gradio for the user interface. Slide 3: Key Features • Text-to-Slide Conversion: Automatically creates slides from a formatted script. • Full Customization: ◦ Control font styles, sizes, and colors for every element. ◦ Use built-in themes or upload your own `.pptx` template. • Intelligent Layouts: ▪ Differentiates between title slides and content slides. ▪ Supports multi-level bullet points. Slide 4: How It Works • Step 1: Write your content using the 'Slide X:' format. • Step 2: Use the 'Customization' tab to tweak the design. • Step 3: Click 'Create PowerPoint' to generate and download your file. • More Info: https://github.com/gradio-app/gradio Slide 5: Q&A • Questions & Discussion """ BUSINESS_PITCH_EXAMPLE = """ Slide 1: Title Slide • InnovateX: AI-Powered Logistics • Revolutionizing Supply Chain Management Slide 2: The Problem • Global supply chains are inefficient and costly. ◦ Estimated $1.8 Trillion lost annually due to inefficiencies. • Key Pain Points: ◦ Lack of real-time visibility. ◦ High fuel and labor costs. ◦ Manual, error-prone documentation. Slide 3: Our Solution • A unified platform leveraging AI and IoT. • Features: ◦ Predictive route optimization. ◦ Real-time cargo tracking & monitoring. ◦ Automated customs documentation. ▪ Blockchain for secure transactions. Slide 4: Market Opportunity • Global Logistics Market Size: $12 Trillion • Targetable Market (TAM): $500 Billion • Our goal is to capture 2% of the TAM within 5 years. Slide 5: Business Model • Subscription-based SaaS model. • Tiers: ◦ Basic: $99/month/vehicle ◦ Pro: $199/month/vehicle ◦ Enterprise: Custom pricing Slide 6: Meet the Team • Jane Doe, CEO: Ex-Google, Logistics expert. • John Smith, CTO: PhD in AI from MIT. • Contact Us: contact@innovatex.com Slide 7: Q&A """ # --- 3. PRESENTATION GENERATION LOGIC --- def parse_color_to_rgb(color_string): """Converts a color string (hex or rgb) to an RGBColor object.""" if isinstance(color_string, str): if color_string.startswith('#'): return RGBColor.from_string(color_string.lstrip('#')) elif color_string.startswith('rgb'): try: r, g, b = map(int, re.findall(r'\d+', color_string)) return RGBColor(r, g, b) except (ValueError, TypeError): return RGBColor(0, 0, 0) return RGBColor(0, 0, 0) def apply_font_style(font, style_config): """Applies a dictionary of style attributes to a font object.""" for key, value in style_config.items(): if key == 'color': font.color.rgb = value elif key == 'font_size': font.size = Pt(value) elif key == 'font_name': font.name = value else: setattr(font, key, value) def find_placeholder(slide, placeholder_enums): """Finds a placeholder shape on a slide.""" for shape in slide.shapes: if shape.is_placeholder and shape.placeholder_format.type in placeholder_enums: return shape return None def populate_title_slide(slide, lines, style_config): """Populates a title slide by setting the paragraph's default font style.""" title_ph = find_placeholder(slide, [PP_PLACEHOLDER.TITLE, PP_PLACEHOLDER.CENTER_TITLE]) subtitle_ph = find_placeholder(slide, [PP_PLACEHOLDER.SUBTITLE]) title_val = "Title Not Found" subtitle_vals = [line.lstrip('• ').strip() for line in lines if line.strip()] if subtitle_vals: title_val = subtitle_vals.pop(0) if title_ph and title_ph.has_text_frame: tf = title_ph.text_frame tf.clear() p = tf.paragraphs[0] # --- FIX: Style first, then add text --- apply_font_style(p.font, style_config['title']) p.text = title_val p.alignment = PP_ALIGN.CENTER if subtitle_ph and subtitle_ph.has_text_frame: tf = subtitle_ph.text_frame tf.clear() p = tf.paragraphs[0] # --- FIX: Style first, then add text --- apply_font_style(p.font, style_config['subtitle']) p.text = '\n'.join(subtitle_vals) p.alignment = PP_ALIGN.CENTER def populate_content_slide(slide, title, lines, style_config): """Populates a content slide by setting each paragraph's default font style.""" title_ph = find_placeholder(slide, [PP_PLACEHOLDER.TITLE]) body_ph = find_placeholder(slide, [PP_PLACEHOLDER.BODY, PP_PLACEHOLDER.OBJECT]) if title_ph and title_ph.has_text_frame: tf = title_ph.text_frame tf.clear() p = tf.paragraphs[0] # --- FIX: Style first, then add text --- apply_font_style(p.font, style_config['body_title']) p.text = title if body_ph and body_ph.has_text_frame: tf = body_ph.text_frame tf.clear() # Remove the single paragraph left by clear() before adding new ones. if lines and len(tf.paragraphs) > 0: p_element = tf.paragraphs[0]._p p_element.getparent().remove(p_element) for line in lines: clean_line = line.strip() if not clean_line: continue p = tf.add_paragraph() hyperlink_match = HYPERLINK_RE.match(clean_line.lstrip('•◦▪ ')) if hyperlink_match: link_text, url = hyperlink_match.groups() # Hyperlinks must be runs, so they are a special case run = p.add_run() apply_font_style(run.font, style_config['hyperlink']) run.text = f"{link_text}: {url}" run.hyperlink.address = url continue if clean_line.startswith(('•', '◦', '▪')): level = BULLET_MAP.get(clean_line[0], 0) text = clean_line[1:].lstrip() style_key = f'body_level_{level}' # --- FIX: Style first, then add text and set level --- apply_font_style(p.font, style_config.get(style_key, style_config['body_level_0'])) p.text = text p.level = level else: # --- FIX: Style first, then add text and set level --- apply_font_style(p.font, style_config['body_level_0']) p.text = clean_line p.level = 0 def create_presentation_file(content, template_path, style_config): """Main function to create the presentation file from text, a template, and styles.""" try: prs = Presentation(template_path) if template_path else Presentation() except Exception as e: raise gr.Error(f"Could not load the presentation template. Please ensure it's a valid .pptx file. Error: {e}") if template_path and len(prs.slides) > 0: title_layout = prs.slides[0].slide_layout content_layout = prs.slides[1].slide_layout if len(prs.slides) > 1 else prs.slides[0].slide_layout else: # Default layouts for blank presentation title_layout = prs.slide_layouts[0] if len(prs.slide_layouts) > 0 else prs.slide_layouts[5] content_layout = prs.slide_layouts[1] if len(prs.slide_layouts) > 1 else prs.slide_layouts[0] slides_data = re.split(r'\nSlide \d+[a-zA-Z]?:', content, flags=re.IGNORECASE) slides_data = [s.strip() for s in slides_data if s.strip()] if not slides_data: raise gr.Error("The input text does not contain any valid slides. Please use the format 'Slide X:'.") for i in range(len(prs.slides) - 1, -1, -1): rId = prs.slides._sldIdLst[i].rId prs.part.drop_rel(rId) del prs.slides._sldIdLst[i] for i, slide_content in enumerate(slides_data): lines = [line.strip() for line in slide_content.split('\n') if line.strip()] if not lines: continue slide_title_text = lines.pop(0) if i == 0 and "title slide" in slide_title_text.lower(): slide = prs.slides.add_slide(title_layout) populate_title_slide(slide, lines, style_config) else: slide = prs.slides.add_slide(content_layout) populate_content_slide(slide, slide_title_text, lines, style_config) with tempfile.NamedTemporaryFile(delete=False, suffix=".pptx") as tmp: prs.save(tmp.name) return tmp.name def create_themed_template(theme): """Creates a temporary .pptx file with a themed background.""" prs = Presentation() slide_master = prs.slide_masters[0] fill = slide_master.background.fill if theme == "Modern Dark": fill.solid() fill.fore_color.rgb = RGBColor(0x1E, 0x1E, 0x1E) elif theme == "Professional Blue": fill.solid() fill.fore_color.rgb = RGBColor(0xE7, 0xF1, 0xFF) with tempfile.NamedTemporaryFile(delete=False, suffix=".pptx") as tmp: prs.save(tmp.name) return tmp.name def main_interface(*args): """Collects all UI inputs and generates the presentation.""" ( content, template_choice, custom_template_file, title_font, title_size, title_bold, title_color, subtitle_font, subtitle_size, subtitle_bold, subtitle_color, body_title_font, body_title_size, body_title_bold, body_title_color, L0_font, L0_size, L0_bold, L0_color, L1_font, L1_size, L1_bold, L1_color, L2_font, L2_size, L2_bold, L2_color ) = args if not content: raise gr.Error("Presentation content is empty. Please enter your text first.") template_path = None if custom_template_file is not None: template_path = custom_template_file.name elif template_choice != "Default White": template_path = create_themed_template(template_choice) hyperlink_style = DEFAULT_STYLES['hyperlink'].copy() hyperlink_style['color'] = parse_color_to_rgb(hyperlink_style['color']) style_config = { 'title': {'font_name': title_font, 'font_size': title_size, 'bold': title_bold, 'color': parse_color_to_rgb(title_color)}, 'subtitle': {'font_name': subtitle_font, 'font_size': subtitle_size, 'bold': subtitle_bold, 'color': parse_color_to_rgb(subtitle_color)}, 'body_title': {'font_name': body_title_font, 'font_size': body_title_size, 'bold': body_title_bold, 'color': parse_color_to_rgb(body_title_color)}, 'body_level_0': {'font_name': L0_font, 'font_size': L0_size, 'bold': L0_bold, 'color': parse_color_to_rgb(L0_color)}, 'body_level_1': {'font_name': L1_font, 'font_size': L1_size, 'bold': L1_bold, 'color': parse_color_to_rgb(L1_color)}, 'body_level_2': {'font_name': L2_font, 'font_size': L2_size, 'bold': L2_bold, 'color': parse_color_to_rgb(L2_color)}, 'hyperlink': hyperlink_style } output_path = create_presentation_file(content, template_path, style_config) return output_path # --- 4. GRADIO UI --- with gr.Blocks(theme=gr.themes.Soft(), css="footer {display: none !important}") as app: gr.Markdown("""
Craft stunning presentations from simple text. Customize everything.
OR
") template_upload = gr.File(label="Upload a Custom .pptx Template", file_types=[".pptx"]) with gr.Accordion("💡 Template Tips", open=False): gr.Markdown(""" - An uploaded template will **override** the built-in choice. - All existing slides in your template will be **removed** and replaced with the new content. - The design (master slide) of your template will be preserved. - For best results, use a template with standard 'Title' and 'Title and Content' layouts. """) gr.Markdown("### 3. Create & Download") create_ppt_btn = gr.Button("🚀 Generate PowerPoint", variant="primary", scale=2) output_file = gr.File(label="Download Your Presentation", interactive=False) with gr.TabItem("🎨 Font & Style Customization"): gr.Markdown("### Fine-tune the look and feel of your presentation text.") with gr.Accordion("Title & Subtitle Styles", open=True): with gr.Row(): title_font = gr.Dropdown(COMMON_FONTS, label="Title Font", value=DEFAULT_STYLES['title']['font_name']) title_size = gr.Slider(10, 100, label="Title Size (pt)", value=DEFAULT_STYLES['title']['font_size'], step=1) title_bold = gr.Checkbox(label="Bold", value=DEFAULT_STYLES['title']['bold']) title_color = gr.ColorPicker(label="Color", value=DEFAULT_STYLES['title']['color']) with gr.Row(): subtitle_font = gr.Dropdown(COMMON_FONTS, label="Subtitle Font", value=DEFAULT_STYLES['subtitle']['font_name']) subtitle_size = gr.Slider(10, 60, label="Subtitle Size (pt)", value=DEFAULT_STYLES['subtitle']['font_size'], step=1) subtitle_bold = gr.Checkbox(label="Bold", value=DEFAULT_STYLES['subtitle']['bold']) subtitle_color = gr.ColorPicker(label="Color", value=DEFAULT_STYLES['subtitle']['color']) with gr.Accordion("Content Body Styles", open=True): with gr.Row(): body_title_font = gr.Dropdown(COMMON_FONTS, label="Slide Title Font", value=DEFAULT_STYLES['body_title']['font_name']) body_title_size = gr.Slider(10, 80, label="Slide Title Size (pt)", value=DEFAULT_STYLES['body_title']['font_size'], step=1) body_title_bold = gr.Checkbox(label="Bold", value=DEFAULT_STYLES['body_title']['bold']) body_title_color = gr.ColorPicker(label="Color", value=DEFAULT_STYLES['body_title']['color']) gr.HTML("