欢迎使用CSDN-markdown编辑器

python通过win32print操作打印机

import PIL.Image
import PIL.ImageWin
import visitor

import win32print
import win32ui

PRINTING_NAME = "win32"
""" The printing name """

PRINTING_SCALE = 4
""" The printing scale """

TEST_TITLE = "colony_test_document"
""" The test title """

TEST_TEXT = "Hello world from Hive Colony"
""" The test text """

HORIZONTAL_RESOLUTION = 8
""" The horizontal resolution """

VERTICAL_RESOLUTION = 10
""" The vertical resolution """

LOG_PIXELS_X = 88
""" The number of dots per inch in the x axis """

LOG_PIXELS_Y = 90
""" The number of dots per inch in the y axis """

PHYSICAL_WIDTH = 110
""" The physical width area """

PHYSICAL_HEIGHT = 111
""" The physical height area """

PHYSICAL_OFFSET_X = 112
""" The physical offset x, the left margin """

PHYSICAL_OFFSET_Y = 113
""" The physical offset y, the top margin """

INCH = 1440
""" The number of inches """


def get_printer_handler(printing_options):
    """
    Retrieves a new printer handler for the
    given printing options.

    @type printing_options: Dictionary
    @param printing_options: The printing options to be used
    to create the printer handler.
    @rtype: Tuple
    @return: The tuple containing the printer handler.
    """

    # retrieves the printer name
    printer_name = printing_options.get("printer_name", win32print.GetDefaultPrinter())

    # creates a new win32 device context and retrieves the handler
    handler_device_context = win32ui.CreateDC()

    # creates a printer device context using the handler
    handler_device_context.CreatePrinterDC(printer_name)

    # retrieves the printer size
    printer_size = printing_options.get("printer_size", get_default_printer_size(handler_device_context))

    # retrieves the printable area
    printable_area = printing_options.get("printable_area", get_default_printable_area(handler_device_context))

    # retrieves the printer margins
    printer_margins = printing_options.get("printer_margins", get_default_printer_margins(handler_device_context))

    # returns the printer handler tuple
    return (
        handler_device_context,
        printable_area,
        printer_size,
        printer_margins
    )


def close_printer_handler(printer_handler_context):
    """
    Closes the printer handler for the given printer
    handler context.

    @type handler_device_context: PyCDC
    @param handler_device_context: The handler to the device (printer) context
    to be closed.
    """

    # releases the resource
    printer_handler_context.DeleteDC()


def get_default_printable_area(handler_device_context):
    """
    Retrieves the default printable area from the given
    device context handler.

    @type handler_device_context: PyCDC
    @param handler_device_context: The handler to the device context.
    @rtype: Tuple
    @return: A tuple containing the printable area.
    """

    return handler_device_context.GetDeviceCaps(HORIZONTAL_RESOLUTION), handler_device_context.GetDeviceCaps(
        VERTICAL_RESOLUTION)


def get_default_printer_size(handler_device_context):
    return handler_device_context.GetDeviceCaps(PHYSICAL_WIDTH), handler_device_context.GetDeviceCaps(
        PHYSICAL_HEIGHT)


def get_default_printer_margins(handler_device_context):
    return handler_device_context.GetDeviceCaps(PHYSICAL_OFFSET_X), handler_device_context.GetDeviceCaps(
        PHYSICAL_OFFSET_Y)


def print_test_image(image_path, printing_options={}):
    # retrieves the printer handler
    handler_device_context, _printable_area, printer_size, printer_margins = get_printer_handler(printing_options)

    # opens the bitmap image
    bitmap_image = PIL.Image.open(image_path)
    # retrieves the bitmap image width and height
    bitmap_image_width, bitmap_image_height = bitmap_image.size

    # starts the document
    handler_device_context.StartDoc(TEST_TITLE)

    # starts the first page
    handler_device_context.StartPage()

    # creates the dib image from the original
    # bitmap image, created with PIL
    dib_image = PIL.ImageWin.Dib(bitmap_image)

    # retrieves the output for the handler device context
    handler_device_context_output = handler_device_context.GetHandleOutput()

    # draws the image in the output for the handler device context
    dib_image.draw(handler_device_context_output, (5, 5, 300, 230))

    # ends the current page
    handler_device_context.EndPage()

    # ends the document
    handler_device_context.EndDoc()

    # closes the printer handler
    close_printer_handler(handler_device_context)

if __name__ == '__main__':
    print_test_image('qr-code.png')
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值