1

I'am using python 3.10 and win32api to send print job to printer, I could change somes settings (set tray) before printing and it works fine, the probleme is : I couldn't update setting for each page, I browse pdf using pymupdf but it seems there is no way to print a memory page, and I couldn't find how to set page range before send print job.

import win32print
import win32gui
import win32api
import fitz

# Constants from wingdi.h
DM_OUT_BUFFER = 0x02
DM_IN_BUFFER = 0x08
DM_IN_PROMPT = 0x04
DM_DEFAULT_SOURCE = 0x200

# Get a handle for the default printer
device_name = win32print.GetDefaultPrinter()
handle = win32print.OpenPrinter(device_name)

# Get the default properties for the printer
properties = win32print.GetPrinter(handle, 2)
devmode = properties['pDevMode']

# Change the default paper source to '4' for 'Manual feed' # Magasin 1
# Change the default paper source to '7' for 'Automatically select' # Magasin 2
filename = 'file3.pdf'
doc = fitz.open(filename)  
    
for page in doc.pages(1, 3, 2):
    devmode.DefaultSource = 4
    devmode.Fields = devmode.Fields | DM_DEFAULT_SOURCE
    win32print.DocumentProperties(None, handle, device_name, devmode, devmode, DM_IN_BUFFER | DM_OUT_BUFFER)

    win32api.ShellExecute (
      0,
      "printto",
      page,
      '"%s"' % win32print.GetDefaultPrinter (),
      ".",
      0
    )

for page in doc.pages(0, 3, 2):
    devmode.DefaultSource = 7
    devmode.Fields = devmode.Fields | DM_DEFAULT_SOURCE
    win32print.DocumentProperties(None, handle, device_name, devmode, devmode, DM_IN_BUFFER | DM_OUT_BUFFER)

    win32api.ShellExecute (
      0,
      "printto",
      page,
      '"%s"' % win32print.GetDefaultPrinter (),
      ".",
      0
    )

the output error :

win32api.ShellExecute (
TypeError: Objects of type 'Page' can not be converted to Unicode.
khelili miliana
  • 3,730
  • 2
  • 15
  • 28

0 Answers0