Files
digiserver/pptx_to_images.py
2025-06-26 21:02:04 +03:00

36 lines
1.4 KiB
Python

import os
import sys
import subprocess
def convert_pptx_to_images(input_file, output_folder):
"""
Converts a PowerPoint file (.ppt or .pptx) to images using LibreOffice.
Each slide is saved as a separate image in the output folder.
"""
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Convert the PowerPoint file to images using LibreOffice
command = [
'libreoffice',
'--headless',
'--convert-to', 'png',
'--outdir', output_folder,
input_file
]
try:
subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(f"PPTX file converted to images: {input_file}")
except subprocess.CalledProcessError as e:
print(f"Error converting PPTX to images: {e.stderr.decode()}")
return False
# Rename the generated images to follow the naming convention
base_name = os.path.splitext(os.path.basename(input_file))[0]
png_files = sorted([f for f in os.listdir(output_folder) if f.endswith('.png') and base_name in f])
for i, file_name in enumerate(png_files):
new_name = f"{base_name}_{i + 1}.png"
os.rename(os.path.join(output_folder, file_name), os.path.join(output_folder, new_name))
print("Renamed slide images:", [f"{base_name}_{i + 1}.png" for i in range(len(png_files))])
return True