Untitled

 avatar
unknown
python
a year ago
4.9 kB
3
Indexable
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
import json
import textwrap

def create_smart_presentation_from_json(data, output_file):
    prs = Presentation()
    
    # Iterate through lessons
    for lesson in data['lessons']:
        # Add lesson title slide
        add_title_slide(prs, lesson['title'])
        
        # Iterate through pages in the lesson
        for page in lesson['pages']:
            add_title_slide(prs, page['title'], "\n".join(page['subtitles']))
            
            # Check content length and split if necessary
            content_chunks = split_content(page['content'])
            for chunk in content_chunks:
                add_text_slide(prs, chunk)
    
    prs.save(output_file)

def split_content(content, max_chars=500):
    # Split content into chunks that fit within the slide
    return textwrap.wrap(content, max_chars)

def add_title_slide(prs, title, subtitle=""):
    layout = prs.slide_layouts[0]  # Title Slide layout
    slide = prs.slides.add_slide(layout)
    title_shape = slide.shapes.title
    subtitle_shape = slide.placeholders[1]

    title_shape.text = title
    subtitle_shape.text = subtitle

def add_text_slide(prs, content):
    layout = prs.slide_layouts[5]  # Blank Slide layout
    slide = prs.slides.add_slide(layout)

    title_box = slide.shapes.add_textbox(Inches(0.5), Inches(0.5), Inches(9), Inches(1))
    title_frame = title_box.text_frame
    title_frame.text = "Content"
    title_frame.paragraphs[0].font.size = Pt(24)
    title_frame.paragraphs[0].font.bold = True
    title_frame.paragraphs[0].font.color.rgb = RGBColor(0, 51, 102)  # Dark blue color

    left = Inches(0.5)
    top = Inches(1.5)
    width = Inches(9)
    height = Inches(5)
    textbox = slide.shapes.add_textbox(left, top, width, height)
    tf = textbox.text_frame

    # Adjust text size based on content length
    font_size = calculate_font_size(content)
    tf.text = content
    for paragraph in tf.paragraphs:
        paragraph.font.size = Pt(font_size)
        paragraph.font.color.rgb = RGBColor(0, 0, 0)  # Black color

def calculate_font_size(content, base_size=18, min_size=12, max_size=24):
    length = len(content)
    if length > 1000:
        return min_size
    elif length > 500:
        return base_size
    else:
        return max_size

# JSON data
json_data = """
{
  "subject": "python",
  "lessons": [
    {
      "title": "Python Basics",
      "pages": [
        {
          "title": "Introduction to Python",
          "subtitles": [
            "Why Learn Python?"
          ],
          "content": "Python is a high-level, interpreted programming language known for its readability and simplicity. It is widely used for web development, data analysis, machine learning, and automation. Python's syntax is concise and easy to learn, making it an ideal language for beginners."
        },
        {
          "title": "Setting Up Your Python Environment",
          "subtitles": [
            "Installing Python",
            "Using an Integrated Development Environment (IDE)",
            "Basic Python Commands"
          ],
          "content": "To start coding in Python, you'll need to install the Python interpreter on your computer. Popular IDEs for Python include PyCharm, Jupyter Notebook, and Visual Studio Code. Once Python is installed, you can run basic commands such as print() and input() to display output and receive user input."
        }
      ]
    },
    {
      "title": "Python Data Types and Control Structures",
      "pages": [
        {
          "title": "Python Data Types",
          "subtitles": [
            "Numbers",
            "Strings",
            "Lists"
          ],
          "content": "Python supports various data types including numbers (integers, float, complex), strings, lists (ordered, mutable), tuples (ordered, immutable), and dictionaries (unordered, mutable). Understanding these data types is crucial for writing effective Python code."
        },
        {
          "title": "Python Control Structures",
          "subtitles": [
            "Conditional Statements",
            "Loops",
            "Functions"
          ],
          "content": "Python includes control structures such as if/else statements and while/for loops for conditional execution of code. Functions are reusable blocks of code that can accept arguments and return values, making them a powerful tool for organizing and simplifying Python programs."
        }
      ]
    }
  ]
}
"""

data = json.loads(json_data)

# Create the presentation
create_smart_presentation_from_json(data, 'smart_presentation.pptx')
# note - need -> pip install python-pptx

Editor is loading...
Leave a Comment