如何使用 python 将从 PDF 中提取的数据/文本打印到热敏或虚拟打印机?

问题描述 投票:0回答:1

我已经成功编写了一个简单的算法来使用 python 在 PDF 上获取文本。我很想将输出打印到热敏/虚拟打印机。我该怎么做?

我尝试过使用 win32print 库。我想将输出打印到打印机。

python printing thermal-printer barcode-printing
1个回答
0
投票

您可以尝试此代码,它仍然使用 win32print 并且应该可以工作,请让我知道您遇到的错误:

import win32print
import win32ui
from pywin32.win32.lib import win32con

def print_text(printer_name, text):
    # Get a handle to the specified printer (or the default printer if None)
    if printer_name:
        printer_handle = win32print.OpenPrinter(printer_name)
    else:
        printer_handle = win32print.OpenPrinter(win32print.GetDefaultPrinter())
    
    try:
        # Start a document and page
        hDC = win32ui.CreateDC()
        hDC.CreatePrinterDCFromHandle(printer_handle)
        hDC.StartDoc('Printing Document')
        hDC.StartPage()

        # Print the text
        hDC.TextOut(100, 100, text)  # You might need to adjust positioning and line breaks for your printer

        # End the page and document
        hDC.EndPage()
        hDC.EndDoc()
    finally:
        win32print.ClosePrinter(printer_handle)

# Example usage
text_to_print = "Hello, this is a test print."
# Use None for the default printer or specify a printer name
print_text(None, text_to_print)

© www.soinside.com 2019 - 2024. All rights reserved.