python
调用打印机需要使用到pywin32
库,使用win32api.ShellExecute
可以调用默认打开指定文档的程序进行打印
调用默认打印机进行打印
import tempfile
import win32api
import win32print
filename = tempfile.mktemp (".txt")
open (filename, "w").write ("This is a test")
win32api.ShellExecute (
0,
"print",#这个参数为open可以调用默认程序打开指定文件,为
filename,
#
# If this is None, the default printer will
# be used anyway.
#
'/d:"%s"' % win32print.GetDefaultPrinter (),
".",
0
)
将第二个参数改成printto
可以调用指定打印机进行打印,第四个参数是指定打印机名
import tempfile
import win32api
import win32print
filename = tempfile.mktemp (".txt")
open (filename, "w").write ("This is a test")
win32api.ShellExecute (
0,
"printto",
filename,
'"%s"' % '指定打印机名',
".",
0
)
获取打印机列表list(map(lambda x:x[2], win32print.EnumPrinters(2)))
更多win32api参照:http://timgolden.me.uk/pywin32-docs/win32_modules.html
网友评论