Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion gramex/pptgen2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Copy link
Contributor

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.


# if output is a pdf, use soffice to convert pptx to pdf, remove pptx file
if file_ext in ['.pdf', '.png'] and pres_name:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer if file_ext in {'.pdf', '.png'} -- i.e. set is faster than list to search in.

Nest this under if target:. You don't need to run this code unless target is specified. Then you don't need to check for pres_name

abs_path = os.path.split(os.path.abspath(target))[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if soffice is in the path using shutilwhich.which('soffice')

If it's not there, use app_log.error() to report an error.

subprocess.call(['soffice', '--headless', '--convert-to', 'pdf', f'{pres_name}',
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pdf_name will not be required.

subprocess.call(['magick', 'convert', f'{pdf_name}', f'{file_name}-%d.png'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check using shutilwhich.which. Use subprocess.run

os.remove(pdf_name)
return prs


Expand Down