Python win32打印示例
2024-10-18 02:33:14
# -*- coding:utf-8 -*-
# Author: Pete Yim<xpHook@gmail.com>
# Date : 13-8-22
# Copyright (c) 2013 RCSH Systems Incorporated. All rights reserved.
import win32print
import win32ui
from PIL import Image, ImageWin
import _imaging #
# Constants for GetDeviceCaps
#
#
# HORZRES / VERTRES = printable area
#
HORZRES = 8
VERTRES = 10
#
# LOGPIXELS = dots per inch
#
LOGPIXELSX = 88
LOGPIXELSY = 90
#
# PHYSICALWIDTH/HEIGHT = total area
#
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
#
# PHYSICALOFFSETX/Y = left / top margin
#
PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113 printer_name = win32print.GetDefaultPrinter ()
file_name = "E:\\abc.jpg" #
# You can only write a Device-independent bitmap
# directly to a Windows device context; therefore
# we need (for ease) to use the Python Imaging
# Library to manipulate the image.
#
# Create a device context from a named printer
# and assess the printable size of the paper.
#
hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC (printer_name)
printable_area = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps (VERTRES)
printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT)
printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY) #
# Open the image, rotate it if it's wider than
# it is high, and work out how much to multiply
# each pixel by to get it as big as possible on
# the page without distorting.
#
bmp = Image.open (file_name)
if bmp.size[0] > bmp.size[1]:
bmp = bmp.rotate (90) ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
scale = min (ratios) #
# Start the print job, and draw the bitmap to
# the printer device at the scaled size.
#
hDC.StartDoc (file_name)
hDC.StartPage () dib = ImageWin.Dib (bmp)
scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
x1 = int ((printer_size[0] - scaled_width) / 2)
y1 = int ((printer_size[1] - scaled_height) / 2)
x2 = x1 + scaled_width
y2 = y1 + scaled_height
dib.draw (hDC.GetHandleOutput (), (x1, y1, x2, y2)) hDC.EndPage ()
hDC.EndDoc ()
hDC.DeleteDC ()
注:python调用win32接口打印照片
最新文章
- UILabel实现自适应高宽
- ggplot2.multiplot:将多个图形使用GGPLOT2在同一页上
- DirectWrite文字排版——字符串去尾
- winform之excel导入和导出
- RSA的密钥把JAVA格式转换成C#的格式
- 明修栈道,暗渡陈仓----之私募一哥徐翔新玩法 z
- Installutil.exe 注册exe
- Android用户界面 UI组件--AdapterView及其子类(二) AdapterViewAnimator及其子类
- IOS开发,如何用最新的Itunes给手机装ipa文件
- velocity序列动画
- USB OTG to PC USB API简介
- Sqoop mysql 数据导入到hdfs
- Swift语言Auto Layout入门教程:上篇
- acm课程练习2--1001
- 最大流&;流分布&;不确定图网络可靠性
- form表单参数传递和url参数传递的区别
- ClientAbortException: java.net.SocketException: Software caused connection abort: socket write erro
- Screen Space Depth Varying Glow based on Heat Diffusion
- 关于xampp中无法启动mysql,Attempting to start MySQL service...的解决办法!!
- 西门子PLC SCL语言开发学习笔记(二)
热门文章
- php 简单的验证码
- 【转】Ext.ajax.request 中的success和failure
- js 不可变的原始值和可变的对象引用
- php版网易视频云api
- HTML 5 video 视频标签全属性详解
- SQL SERVER 2008 使用TDE加密和解密
- “typedef int (init_fnc_t) (void);“的含义
- iOS开发之国际化
- The state of Web Components
- [wikioi]关押罪犯