在Python中,可以使用第三方庫win32print
來調用打印機。首先,需要安裝這個庫,可以使用pip來安裝:
pip install pypiwin32
然后,可以使用如下代碼來調用打印機:
import win32print
import win32api
printer_name = win32print.GetDefaultPrinter()
hprinter = win32print.OpenPrinter(printer_name)
printer_info = win32print.GetPrinter(hprinter, 2)
# 打印文檔
data = "Hello, World!"
job = win32print.StartDocPrinter(hprinter, 1, ("Test", None, "RAW"))
win32print.StartPagePrinter(hprinter)
win32api.WritePrinter(hprinter, data)
win32print.EndPagePrinter(hprinter)
win32print.EndDocPrinter(hprinter)
win32print.ClosePrinter(hprinter)
這段代碼會將"Hello, World!"打印到默認的打印機上。可以根據需要更改文檔內容和打印機名稱。