Skip to content
PodSubs
ShowsPeopleGuestsJobsNewsEvents
More
BlogPodCred
Menu
ShowsPeopleGuestsJobsNewsEventsBlogPodCredNetworksSign in
Join nowSign in
  • Shows
  • People
  • Jobs
  • News
  • More
    • Guests
    • Events
    • Blog
    • PodCred
    • Networks
    Join nowSign in

Daily Python Projects Podcast

Daily Python Projects Podcast artwork

Build a Private PDF Toolkit with Python and CustomTkinter: Day 2 - Compress, Rotate and Protect PDFs, and Build an .exe

Nov 4, 20261 minAudio

This is a free preview of a paid episode. To hear more, visit dailypythonprojects.substack.com Yesterday we built a desktop app that merges and splits PDFs on your own computer. Today it gets three more tools: Compress, Rotate and Protect. Then we turn the app into a program you can double-click, so you can give it to people who don’t have Python. The video above shows how the code of the app is organized: why there is a class, why some functions sit outside it, and how one click on a button becomes a new PDF on your disk. Projects in this week’s series: Day 1: Yesterday, we built a desktop app with CustomTkinter that merges and splits PDF files. Day 2 (Today): We add compress, rotate and password protection, and we turn the app into a program for Windows and Mac. View All Projects This Week Get the data Today we use a scan of the signed rental contract. Please download it below and save it anywhere on your computer: And here is how the scan looks inside. It is 8.4 MB for four pages, and page 3 came out sideways: The output With the three new tabs, the scan becomes a small, upright PDF with a password, ready to send by email: And this is the new Compress tab, right after it made the file 94% smaller: What you get today * A Compress tab that makes scans and photos smaller, with three levels * A Rotate tab that turns pages that were scanned sideways or upside down * A Protect tab that locks a PDF with an AES-256 password * A step by step guide to turn the app into a program for Windows and Mac The key idea A scanned PDF is big because every page is a photo. Our scan has four photos of 2481 by 3508 pixels, saved as high quality JPEGs. Your screen does not need that many pixels, so Compress makes every photo smaller and saves it again with a lower JPEG quality. These are the main lines of compress_to(): writer = PdfWriter(clone_from=path) for page in writer.pages: for image in page.images: picture = image.image picture.thumbnail((max_size, max_size)) image.replace(picture, quality=quality) writer.write(target) PdfWriter(clone_from=path) makes a copy of the PDF that we can change. Then page.images gives us every picture on a page, and image.image is that picture as a Pillow image. Pillow’s thumbnail() shrinks the picture so that its longest side is at most max_size pixels, and it keeps its shape. Finally, replace() puts it back into the PDF as a JPEG with the quality we chose. On Medium, max_size is 2200 and the quality is 60. Each photo goes from about 2.1 MB to about 120 KB, and the whole file goes from 8.4 MB to 476 KB. Worth knowing * Compress helps with scans and photos, but not with PDFs that are mostly text. Our invoice from Tuesday did not get smaller at all, and the app says so in the status line. * Rotate does not change the photo. Every page has a /Rotate value that tells the PDF reader how to show it, and page.rotate(90) only changes that value. That is why it is instant and loses no quality. * A protected PDF has no reset button. If you forget the password, the file cannot be opened anymore. Also, AES-256 is only as strong as the password, because a program can try millions of short passwords, so use a long one. Run it Install the libraries and run the app (you can find the code at the bottom of the post): pip install customtkinter pypdf pillow cryptography python pdf_toolkit.py Pillow is needed for Compress, and cryptography is needed for the AES-256 encryption in Protect. Now try the three new tabs with the scan. In the Rotate tab, choose scanned_contract.pdf and type 3: Then compress the rotated file on Medium. Finally, choose the compressed file in the Protect tab and type a password two times: Make a program you can double-click So far, the app needs Python to run. PyInstaller packs your script, a copy of Python and all the libraries into one program. So you can give the app to people who have never installed Python. These are the steps: 1. Make a clean virtual environment. PyInstaller packs every library that your app imports, and also the libraries those libraries use when they are installed. For example, when the environment also has numpy, the program grows from 25 MB to 44 MB. So open a terminal in the folder of pdf_toolkit.py and run: # Windows python -m venv venv venv\Scripts\activate # Mac python3 -m venv venv source venv/bin/activate # both pip install customtkinter pypdf pillow cryptography pyinstaller 2. Add an icon. Save this image as icon.png in the same folder as pdf_toolkit.py. You can also use any other square PNG image: 3. Build the program. Run the command for your system: # Windows pyinstaller --onefile --windowed --name "PDF Toolkit" --icon icon.png pdf_toolkit.py # Mac pyinstaller --windowed --name "PDF Toolkit" --icon icon.png pdf_toolkit.py This is what the options do. --windowed hides the black terminal window that would open behind the app. --name sets the name of the program, and --icon sets its icon. PyInstaller uses Pillow to turn the PNG into the .ico file that Windows needs, or the .icns file that a Mac needs. On Windows, --onefile puts everything into one .exe file. On a Mac we leave it out, because a Mac app is a folder anyway, and PyInstaller warns that this combination will stop working in version 7. 4. Open it. The build takes about half a minute. When the last line says Build complete!, your program is in the dist folder: dist\PDF Toolkit.exe on Windows, and dist/PDF Toolkit.app on a Mac. Double-click it. On a Mac, you can also drag it into your Applications folder. The build folder and the PDF Toolkit.spec file are only used for building, so you don’t need to send them to anyone. 5. Send it to someone. On Windows, send the .exe file. On a Mac, right-click the app and choose Compress first, because a Mac app is a folder, and then send the .zip file. 6. Open it on the other computer. The first time, the other person sees a warning, because the program is not signed by a known developer. That is normal for a program you made yourself: * Windows: a blue window says “Windows protected your PC”. Click More info, and then Run anyway. * Mac: macOS says that Apple could not verify the app. Close the message, but don’t move the app to the Trash. Then open System Settings, choose Privacy & Security, scroll down to Security, and click Open Anyway. Apple says this button is there for about an hour after you tried to open the app. Two more things. You build the Windows program on Windows and the Mac program on a Mac, because PyInstaller cannot build for another system. And a Mac app built on an Apple Silicon Mac does not run on older Intel Macs. Try this Add a fourth level to COMPRESS_LEVELS called “Tiny”, with quality 30 and 1200 pixels. How small does the scan get, and can you still read the signatures? Reply and tell me. Solution The full code is below. It is one file, pdf_toolkit.py, and every step has a comment.

Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.

Hosts and Guests

Nobody is credited on this episode yet.

Credit the people on this episode

Be the first to rate this episode

More episodes

All episodes
  1. Build a Sales Report Robot with Python and Polars: Day 2 - Email the Report and Run It Every Monday

    Oct 28, 20261 minAudio

    This is a free preview of a paid episode. To hear more, visit dailypythonprojects.substack.com Yesterday we turned four Excel files into a PDF report with Polars. Today the robot sends that PDF by email, and the...

    Credit the people on this episode

  2. Read a 1 GB Log File with Python Generators: Day 2 - Generator Pipelines and a Live Log Watcher

    Oct 21, 20261 minAudio

    This is a free preview of a paid episode. To hear more, visit dailypythonprojects.substack.com Yesterday we read a 1 GB log file with one generator. Today we connect small generators into a pipeline, and point it at a...

    Credit the people on this episode

  3. How to use Daily Python Projects

    Aug 21, 20242 minAudio

    This is a public episode. If you'd like to discuss this with other subscribers or get access to bonus episodes, visit dailypythonprojects.substack.com/subscribe

    Credit the people on this episode

  4. How to Learn Python

    Aug 16, 20246 minAudio

    This is a public episode. If you'd like to discuss this with other subscribers or get access to bonus episodes, visit dailypythonprojects.substack.com/subscribe

    Credit the people on this episode

  5. For-loops thoroughly explained for beginners

    Aug 9, 202418 secAudio

    This is a free preview of a paid episode. To hear more, visit dailypythonprojects.substack.com

    Credit the people on this episode