新しい名前付きオブジェクトを使用してオブジェクトを操作することで、pDevMode
オブジェクトを編集することもできます。win32print.GetPrinter()
import win32print, os
name = win32print.GetDefaultPrinter()
printdefaults = {"DesiredAccess": win32print.PRINTER_ACCESS_USE}
handle = win32print.OpenPrinter(name, printdefaults)
level = 2
attributes = win32print.GetPrinter(handle, level)
# http://timgolden.me.uk/pywin32-docs/PyDEVMODE.html
# Note: All pDevMode settings are int() variables
attributes['pDevMode'].Copies = 2 # Num of copies
#attributes['pDevMode'].Color = 1 # Color
attributes['pDevMode'].Color = 2 # Monochrome
attributes['pDevMode'].Collate = 1 # Collate TRUE
#attributes['pDevMode'].Collate = 2 # Collate FALSE
同様の質問で、Yuri Gendelman によって提供された「属性」命名構造を拡張しました: Print PDF file in duplex mode via Python
これは、私のコードでそれをどのように使用したかのサンプルです。
import win32print, os
def autoprint(user):
name = win32print.GetDefaultPrinter()
printdefaults = {"DesiredAccess": win32print.PRINTER_ACCESS_USE}
handle = win32print.OpenPrinter(name, printdefaults)
level = 2
attributes = win32print.GetPrinter(handle, level) # http://timgolden.me.uk/pywin32-docs/PyDEVMODE.html
# All are int() variables
attributes['pDevMode'].Duplex = 1 # no flip
#attributes['pDevMode'].Duplex = 2 # flip up
#attributes['pDevMode'].Duplex = 3 # flip over
attributes['pDevMode'].Copies = 2 # Num of copies
#attributes['pDevMode'].Color = 1 # Color
attributes['pDevMode'].Color = 2 # Monochrome
attributes['pDevMode'].Collate = 1 # Collate TRUE
#attributes['pDevMode'].Collate = 2 # Collate FALSE
try:
win32print.SetPrinter(handle, level, attributes, 0)
except:
print("win32print.SetPrinter: settings could not be changed")
try:
newfile_name = max([downloadPath + "\\" + user["FULL_NAME"] + "PDFToBePrinted.pdf"])
Print2Copies = win32api.ShellExecute(0, 'print', newfile_name, None, '.', 0)
time.sleep(1)
Print2Copies
print("Printing now...")
win32print.ClosePrinter(handle)
final_filename = max([downloadPath + "\\" + user["FULL_NAME"] + "Printed.pdf"])
os.rename(newfile_name, final_filename)
except Exception as e:
print(str(e))
print("--Failed to print--")
time.sleep(5)
デフォルト設定を確認するコードは次のとおりです。
win32print.GetPrinter(handle, level)['pDevMode'].Copies
win32print.GetPrinter(handle, level)['pDevMode'].Duplex
In[115]: print(win32print.GetPrinter(handle, level)['pDevMode'].Copies)
Out[115]: 1
In[116]:win32print.GetPrinter(handle, level)['pDevMode'].Duplex
Out[116]: 1
In[117]:win32print.GetPrinter(handle, level)['pDevMode'].Color
Out[117]: 1
変更できるその他のプリンター設定は次のpDevMode
とおりです。http://timgolden.me.uk/pywin32-docs/PyDEVMODE.html