diff --git a/app.py b/app.py index 2ab5f38..2656aab 100644 --- a/app.py +++ b/app.py @@ -9,11 +9,8 @@ from extensions import db, bcrypt, login_manager from models import User, Player, Content, Group # Add Group to the imports from flask_login import login_user, logout_user, login_required, current_user from pptx import Presentation -from pptx.util import Inches -from PIL import Image -import io -import threading -from PIL import Image, ImageDraw +import cairosvg +import os app = Flask(__name__, instance_relative_config=True) @@ -184,7 +181,7 @@ def convert_video_and_update_playlist(file_path, original_filename, target_type, def convert_pptx_to_images(input_file, output_folder): """ - Converts a PowerPoint file (.pptx) to images using python-pptx. + Converts a PowerPoint file (.pptx) to images while preserving the format and look of the slides. Each slide is saved as a separate image in the output folder. """ try: @@ -196,20 +193,13 @@ def convert_pptx_to_images(input_file, output_folder): base_name = os.path.splitext(os.path.basename(input_file))[0] for i, slide in enumerate(presentation.slides): - # Create a blank image for the slide - img = Image.new('RGB', (1920, 1080), color='white') # Adjust resolution as needed - draw = ImageDraw.Draw(img) + # Generate an SVG representation of the slide + svg_content = generate_svg_from_slide(slide) - # Extract slide content (text, shapes, etc.) - for shape in slide.shapes: - if shape.has_text_frame: - text = shape.text_frame.text - draw.text((50, 50 + i * 20), text, fill='black') # Adjust position and styling - - # Save the image + # Convert the SVG to a high-quality image (JPEG) image_filename = f"{base_name}_slide_{i + 1}.jpg" image_path = os.path.join(output_folder, image_filename) - img.save(image_path, 'JPEG') + cairosvg.svg2png(bytestring=svg_content, write_to=image_path, dpi=300) print(f"Slide {i + 1} saved as image: {image_path}") # Delete the original PPTX file after conversion @@ -222,6 +212,25 @@ def convert_pptx_to_images(input_file, output_folder): print(f"Error processing PPTX file: {e}") return False +def generate_svg_from_slide(slide): + """ + Generates an SVG representation of a PowerPoint slide. + This function extracts shapes, text, and other elements from the slide. + """ + svg_content = '' + for shape in slide.shapes: + if shape.has_text_frame: + text = shape.text_frame.text + # Add text to the SVG (adjust position and styling as needed) + svg_content += f'{text}' + elif shape.shape_type == 13: # Picture shape + if shape.image: + image_bytes = shape.image.blob + image_base64 = base64.b64encode(image_bytes).decode('utf-8') + svg_content += f'' + svg_content += '' + return svg_content + # Convert EMU to pixels def emu_to_pixels(emu): return int(emu / 914400 * 96) diff --git a/requirements.txt b/requirements.txt index efa4f44..2dfa519 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,4 +17,5 @@ typing_extensions==4.12.2 Werkzeug==3.1.3 gunicorn==20.1.0 pdf2image==1.17.0 -python-pptx==0.6.21 \ No newline at end of file +python-pptx==0.6.21 +cairosvg==2.7.0