pythonprintingpywin32acrobatshellexecute

How to print pdfs with the printer property "staple" in python using adobe acrobat


currently I'm trying to automate a few things in my workspace to make things easier. We're receiving a lot of invoices trough our email and I wanted to print and save the pdf into a certain location.

Problem: We have a printer that is able to staple the documents printed but I cant figure out on how to configure the properties of the printer so that it prints them and staples the papers.

Code:

from pathlib import Path
import win32com.client
import time
from os import listdir
from os.path import isfile, join
from winotify import Notification, audio
import win32api
import win32print
import os
import win32con
import win32ui


outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders[email].Folders["folder"].Folders["folder"]
archiv = outlook.Folders[email].Folders["folder"].Folders["folder"]
printer_name = win32print.GetDefaultPrinter()

for Item in inbox.Items:
    subject = Item.Subject
    body = Item.body
    attachments = Item.Attachments
    target_folder = r"C:path"

    try:
        for attachment in attachments:
            if attachment.FileName.endswith(".pdf") or attachment.FileName.endswith(".PDF"):
                print("Printing document:", attachment.FileName)
                temp_file_path = os.path.join(target_folder, attachment.FileName)
                attachment.SaveAsFile(temp_file_path)
                win32api.ShellExecute(0, "print", temp_file_path, f'/d:"{printer_name}" /staple:1', ".", 0)
        Item.Move(archiv)

        toast = Notification(app_id="Invoice Automation",
                     title="Log Notification",
                     msg=f"process was successful: {Item.Subject}",
                     duration="short")
        toast.show()

    except Exception as  e:
        toast = Notification(app_id="Invoice Automation",
                     title="Log Notification",
                     msg=f"Error message: {e}",
                     duration="short")

        toast.show()

I tried a few methods found on the internet but none of them worked. I also tried adding "/staple:1". But of course that also didnt work. My plan was to have it printed on both pages and if there where more than one page for a document to staple them together. This way we wouldnt lose time trying to staple them together by hand.

Thank you so much for your help!


Solution

  • For anyone facing the same issue, I resolved it as follows:

    Rather than attempting to define the parameters when sending the command, I duplicated the printer and adjusted the default settings. Now, I simply choose the new printer instead of the old one. By using this method, I achieved the goal of stapling. Another issue I encountered was printing on both sides of the page, which depends heavily on the app used to open PDFs. For instance, PDF24 doesn't print both sides when simply opening and printing, but Adobe Acrobat does.