Created
May 21, 2021 18:11
-
-
Save 1kastner/53825e77b3593be20064c2fd37374b98 to your computer and use it in GitHub Desktop.
Export a set of PPTX files as a single PDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import datetime | |
import comtypes.client | |
from PyPDF2 import PdfFileMerger | |
target_dir = os.path.abspath(os.path.join( | |
os.path.dirname(os.path.realpath(__file__)), | |
os.pardir, | |
"data transfer" | |
)) | |
print("Export to: " + target_dir) | |
date_prefix = datetime.datetime.now().date().isoformat() | |
sources = [] | |
intermediate_pdf_names = [] | |
themenbloecke_dir = os.path.dirname(os.path.realpath(__file__)) | |
for folder in os.listdir(themenbloecke_dir): | |
if not os.path.isdir(folder): | |
continue | |
if folder.startswith("_") or folder.startswith("00"): # adjust rules as you wish | |
continue | |
for praesi_file in os.listdir(os.path.join(themenbloecke_dir, folder)): | |
if not praesi_file.endswith(".pptx"): | |
continue | |
sources.append( | |
os.path.abspath(os.path.join( | |
themenbloecke_dir, | |
folder, | |
praesi_file | |
) | |
)) | |
intermediate_pdf_names.append( | |
os.path.abspath(os.path.join( | |
themenbloecke_dir, | |
folder, | |
date_prefix + "_" + praesi_file[:-5] + ".pdf" | |
) | |
)) | |
print("Sources: ") | |
for i, source_file in enumerate(sources): | |
print(f"{i+1}) {source_file}") | |
print("Intermediate PDFs: ") | |
for i, intermediate_pdf_name in enumerate(intermediate_pdf_names): | |
print(f"{i+1}) {intermediate_pdf_name}") | |
powerpoint = comtypes.client.CreateObject("Powerpoint.Application") | |
for source, output in zip(sources, intermediate_pdf_names): | |
if os.path.isfile(output): | |
do_overwrite = input(f"Overwrite '{output}'? [n]/y ") | |
if do_overwrite != "y": | |
continue | |
deck = powerpoint.Presentations.Open(source) | |
deck.SaveAs(output, 32) # formatType = 32 for pptx to pdf | |
deck.Close() | |
powerpoint.Quit() | |
for intermediate_pdf in intermediate_pdf_names: | |
if not os.path.isfile(intermediate_pdf): | |
raise Exception(f"Missing file: {intermediate_pdf}") | |
print("PDF exists: " + intermediate_pdf) | |
merger = PdfFileMerger() | |
for pdf in intermediate_pdf_names: | |
merger.append(pdf) | |
time_information = input("Semester: ") | |
export_file_name = f"My Course - {time_information}.pdf" | |
path_to_export_file = os.path.join( | |
target_dir, | |
export_file_name | |
) | |
merger.write(path_to_export_file) | |
merger.close() | |
print(f"Created: {path_to_export_file}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment