Python extension module that provides access to the Windows printing API. Give me sample code
Here is a sample code that demonstrates how to use the win32print module in Python to print a document:
import win32print
# specify the printer name and document to be printed
printer_name = win32print.GetDefaultPrinter()
document_name = "c:\\users\\user\\test.docx"
# open the printer
printer_handle = win32print.OpenPrinter(printer_name)
# start a print job and get the job id
job_info = win32print.StartDocPrinter(printer_handle, 1, (document_name, None, "RAW"))
job_id = job_info[1]
# open the document and read its contents
document_handle = win32print.OpenPrinter(document_name, "rb")
document_contents = document_handle.read()
# write the document to the printer
win32print.StartPagePrinter(printer_handle)
win32print.WritePrinter(printer_handle, document_contents)
win32print.EndPagePrinter(printer_handle)
# end the print job
win32print.EndDocPrinter(printer_handle)
win32print.ClosePrinter(printer_handle)
This code opens the default printer, starts a print job, reads the contents of the specified document, writes the document to the printer, and ends the print job.
原文地址: https://www.cveoy.top/t/topic/wfq 著作权归作者所有。请勿转载和采集!