1

Can I print to a specific printer instead of the default printer with

os.startfile(filename, 'print')

I haven't found anything about the third parameter.

xralf
  • 3,312
  • 45
  • 129
  • 200
  • It doesn't have a 3rd parameter; just file & operation (in your case, `print`). – Scott Hunter Feb 26 '20 at 18:02
  • @ScottHunter So, there is no possibility to print to a specific printer with it? – xralf Feb 26 '20 at 18:18
  • You might be able to open up a subprocess to get the default printer name, then change the printer to the desired printer, print using os.starfile if you so choose, then change it back to the default, but that's the best I got. I am trying to look for a solution as well. – Shmack Jul 13 '20 at 15:39
  • @ShanerM13 I'm printing `pdf` files. I have used [pdftoprinter](https://stackoverflow.com/a/47994723/653379) in the end. – xralf Jul 13 '20 at 17:40

1 Answers1

1

With win32print.EnumPrinters(2) you get all the installed Printers.

This is a python 2.7 interpreter running on a windows10 machine.

import win32api
import win32print
import os
import time
import shutil

#all_printers = win32print.EnumPrinters(2)
defaultPrinter = win32print.GetDefaultPrinter()
if defaultPrinter != 'TSC TC200 UG':
    win32print.SetDefaultPrinter('TSC TC200 UG')
pdf_dir = "Y:\\HOTFOLDER_DRUCK\\TSC_TC200_ETIKETTEN_UG\\INPUT"
archiv = "Y:\\HOTFOLDER_DRUCK\\TSC_TC200_ETIKETTEN_UG\\ARCHIV"

while True:
    files = os.listdir(pdf_dir)
    if files > 0:
        for f in files:
            print "printing file "+ str(pdf_dir+f) +" on "+str(win32print.GetDefaultPrinter())
            win32api.ShellExecute(0, "print", os.path.join(pdf_dir,f), None,  ".",  0)
            time.sleep(6)
            shutil.copy(os.path.join(pdf_dir,f),os.path.join(archiv,f))
            os.remove(os.path.join(pdf_dir,f))