import win32print
def get_printer_list(): printers = win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL | win32print.PRINTER_ENUM_CONNECTIONS) printer_list = [] for printer in printers: printer_name = printer[2] printer_list.append(printer_name) return printer_list
# 調(diào)用函數(shù)獲取打印機(jī)列表 printer_list = get_printer_list() for printer in printer_list: print(printer)
在Python中,你可以使用`win32api`和`win32print`模塊(適用于Windows系統(tǒng))或`cups`模塊(適用于Linux和macOS系統(tǒng))來調(diào)用打印機(jī)進(jìn)行打印操作。 以下是使用這兩個(gè)模塊分別調(diào)用打印機(jī)進(jìn)行打印的示例代碼: **在Windows系統(tǒng)中調(diào)用打印機(jī)打印:** import win32api import win32print
def print_file(filename, printer_name=None): # 打開文件 file = open(filename, 'rb') data = file.read() file.close()
# 獲取默認(rèn)打印機(jī) if not printer_name: printer_name = win32print.GetDefaultPrinter()
# 打印文件 hPrinter = win32print.OpenPrinter(printer_name) try: hJob = win32print.StartDocPrinter(hPrinter, 1, ('print job', None, 'RAW')) try: win32print.StartPagePrinter(hPrinter) win32print.WritePrinter(hPrinter, data) win32print.EndPagePrinter(hPrinter) finally: win32print.EndDocPrinter(hPrinter) finally: win32print.ClosePrinter(hPrinter)
# 調(diào)用函數(shù)進(jìn)行打印 print_file('test.pdf', 'Your Printer Name') 這些示例代碼分別使用了`win32print`和`cups`模塊來調(diào)用打印機(jī)進(jìn)行打印。它們都包含一個(gè)`print_file`函數(shù),該函數(shù)接受要打印的文件名和打印機(jī)名稱作為參數(shù)。 在Windows系統(tǒng)中,`print_file`函數(shù)使用`win32api`和`win32print`模塊打開文件并將其寫入打印機(jī)。在Linux和macOS系統(tǒng)中,`print_file`函數(shù)使用`cups`模塊打印文件并返回打印作業(yè)的ID。 ●GUI太難做不出來,看看這個(gè)庫吧! ●太快了,10秒搭建一個(gè)API請(qǐng)求服務(wù) ●一起學(xué)習(xí)flask打造博客系統(tǒng) |
|