Skip to content

Instantly share code, notes, and snippets.

@lem0n4de
Last active October 24, 2018 14:06
Show Gist options
  • Select an option

  • Save lem0n4de/9bbd5bb626ed10df3441c67b462d4836 to your computer and use it in GitHub Desktop.

Select an option

Save lem0n4de/9bbd5bb626ed10df3441c67b462d4836 to your computer and use it in GitHub Desktop.
Personal python script to copy a folder recursively to another point with powerpoint files saved as pdf.
import comtypes.client
import shutil
from pathlib import Path
# Function is from this answer: https://stackoverflow.com/a/31624001
def PPTtoPDF(inputFileName, outputFileName, formatType = 32):
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
powerpoint.Visible = 1
deck = powerpoint.Presentations.Open(inputFileName)
if deck.ReadOnly:
print(f"{inputFileName} is read-only.")
return
deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
deck.Close()
powerpoint.Quit()
def create_output_dir(dir_name):
full_dir_name = OUTPUT_DIR + "\\" + dir_name
p = Path(full_dir_name)
p.mkdir(exist_ok=True)
return p
# main output directory
OUTPUT_DIR = r"C:\Users\Derslik\Downloads\ORCUN"
def recursive_search_dir(dir_name: Path) -> str:
for item in dir_name.iterdir():
if item.is_dir():
create_output_dir(str(item))
recursive_search_dir(item)
elif item.is_file():
out_dir = OUTPUT_DIR + "\\" + str(item.parent)
out_dir = Path(out_dir)
## print(out_dir)
out_abs = out_dir / item.name
if item.suffix == ".pptx" or item.suffix == ".ppt" or item.suffix == ".pps":
out_abs = out_abs.with_suffix(".pdf")
if out_abs.exists():
print(f"File {out_abs} already exists.")
continue
try:
print(f"Copying file {item.resolve()} to {out_abs}")
PPTtoPDF(str(item.resolve()), str(out_abs))
except:
print(f"{item.name} PDF'ye dönüştürülemedi.")
else:
if out_abs.exists():
print(f"File {out_abs} already exists.")
continue
shutil.copy2(str(item.resolve()), str(out_abs))
print(f"File {item.resolve()} copied to {out_abs}.")
def main():
recursive_search_dir(Path("."))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment