Skip to content

Instantly share code, notes, and snippets.

@0xdade
Created September 29, 2024 19:30
Show Gist options
  • Save 0xdade/8aa1ce545efb57fbe40e8745c638c306 to your computer and use it in GitHub Desktop.
Save 0xdade/8aa1ce545efb57fbe40e8745c638c306 to your computer and use it in GitHub Desktop.
import argparse
import os
import sys
from pypdf import PdfReader, PdfWriter # pip install pypdf
def main():
parser = argparse.ArgumentParser(
description='Simplify PDF by reading each page and writing out to a new file. Does not preserve metadata, permissions, bookmarks, etc.',
)
parser.add_argument('filename', type=str, help='The name of the PDF file to simplify')
args = parser.parse_args()
if not os.path.isfile(args.filename):
print(f"Error: The file '{filename}' does not exist.")
sys.exit(1)
basename, ext = os.path.splitext(args.filename)
outname = f"{basename}-strip{ext}"
reader = PdfReader(args.filename)
with PdfWriter() as w:
for page in reader.pages:
w.add_page(page)
w.write(outname)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment