updated to cairosvg
This commit is contained in:
43
app.py
43
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 models import User, Player, Content, Group # Add Group to the imports
|
||||||
from flask_login import login_user, logout_user, login_required, current_user
|
from flask_login import login_user, logout_user, login_required, current_user
|
||||||
from pptx import Presentation
|
from pptx import Presentation
|
||||||
from pptx.util import Inches
|
import cairosvg
|
||||||
from PIL import Image
|
import os
|
||||||
import io
|
|
||||||
import threading
|
|
||||||
from PIL import Image, ImageDraw
|
|
||||||
|
|
||||||
app = Flask(__name__, instance_relative_config=True)
|
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):
|
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.
|
Each slide is saved as a separate image in the output folder.
|
||||||
"""
|
"""
|
||||||
try:
|
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]
|
base_name = os.path.splitext(os.path.basename(input_file))[0]
|
||||||
|
|
||||||
for i, slide in enumerate(presentation.slides):
|
for i, slide in enumerate(presentation.slides):
|
||||||
# Create a blank image for the slide
|
# Generate an SVG representation of the slide
|
||||||
img = Image.new('RGB', (1920, 1080), color='white') # Adjust resolution as needed
|
svg_content = generate_svg_from_slide(slide)
|
||||||
draw = ImageDraw.Draw(img)
|
|
||||||
|
|
||||||
# Extract slide content (text, shapes, etc.)
|
# Convert the SVG to a high-quality image (JPEG)
|
||||||
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
|
|
||||||
image_filename = f"{base_name}_slide_{i + 1}.jpg"
|
image_filename = f"{base_name}_slide_{i + 1}.jpg"
|
||||||
image_path = os.path.join(output_folder, image_filename)
|
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}")
|
print(f"Slide {i + 1} saved as image: {image_path}")
|
||||||
|
|
||||||
# Delete the original PPTX file after conversion
|
# 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}")
|
print(f"Error processing PPTX file: {e}")
|
||||||
return False
|
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 = '<svg xmlns="http://www.w3.org/2000/svg" width="1920" height="1080">'
|
||||||
|
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 x="50" y="50" font-size="20" fill="black">{text}</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'<image href="data:image/png;base64,{image_base64}" x="0" y="0" width="1920" height="1080"/>'
|
||||||
|
svg_content += '</svg>'
|
||||||
|
return svg_content
|
||||||
|
|
||||||
# Convert EMU to pixels
|
# Convert EMU to pixels
|
||||||
def emu_to_pixels(emu):
|
def emu_to_pixels(emu):
|
||||||
return int(emu / 914400 * 96)
|
return int(emu / 914400 * 96)
|
||||||
|
|||||||
@@ -17,4 +17,5 @@ typing_extensions==4.12.2
|
|||||||
Werkzeug==3.1.3
|
Werkzeug==3.1.3
|
||||||
gunicorn==20.1.0
|
gunicorn==20.1.0
|
||||||
pdf2image==1.17.0
|
pdf2image==1.17.0
|
||||||
python-pptx==0.6.21
|
python-pptx==0.6.21
|
||||||
|
cairosvg==2.7.0
|
||||||
|
|||||||
Reference in New Issue
Block a user