-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Added PPTX to PDF/PNG #390
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
import pandas as pd | ||
import pptx | ||
import sys | ||
import subprocess | ||
from fnmatch import fnmatchcase | ||
from gramex.config import app_log | ||
from gramex.transforms import build_transform | ||
|
@@ -103,8 +104,33 @@ def pptgen(source: Union[str, pptx.presentation.Presentation], | |
slide_data['slide'] = slide # Rule can use 'slide' as a variable | ||
transition(slide, rule.get('transition', None), data) | ||
apply_commands(rule, slide.shapes, slide_data) | ||
|
||
# requirements: install LibreOffice, ImageMagick | ||
# (https://learn.gramener.com/guide/pptxhandler/#pptx-to-images) | ||
|
||
# Ensure both LibreOffice, ImageMagick are updated in the environment variables | ||
# C:\Program Files\LibreOffice\program, C:\Program Files\ImageMagick-7.0.11-Q16-HDRI | ||
# Add fonts to LiberOffice if they are not available by default | ||
|
||
# Get filename, extension, save the file as pptx first | ||
pres_name, pdf_name, file_ext = None, None, None | ||
if target: | ||
prs.save(target) | ||
file_name, file_ext = os.path.splitext(target) | ||
pres_name = f'{file_name}.pptx' | ||
prs.save(pres_name) | ||
|
||
# if output is a pdf, use soffice to convert pptx to pdf, remove pptx file | ||
if file_ext in ['.pdf', '.png'] and pres_name: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer Nest this under |
||
abs_path = os.path.split(os.path.abspath(target))[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if If it's not there, use |
||
subprocess.call(['soffice', '--headless', '--convert-to', 'pdf', f'{pres_name}', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use subprocess.run instead. |
||
'--outdir', f'{abs_path}']) | ||
pdf_name = file_name + '.pdf' | ||
os.remove(pres_name) | ||
|
||
# if output is a png, use the image magick with pdf generated in previous step to convert to png, remove pdf file | ||
if file_ext == '.png' and pdf_name: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
subprocess.call(['magick', 'convert', f'{pdf_name}', f'{file_name}-%d.png']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check using |
||
os.remove(pdf_name) | ||
return prs | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
prs.save(target)
-- what if target has a.PPTX
in caps? Just retain the original.Instead, just extract the
file_ext
-- convert it to lowercase.