Python win32打印示例
- # -*- 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接口打印照片
Python win32打印示例的更多相关文章
- 将python代码打印成pdf
将python代码打印成pdf,打印出来很丑,完全不能看. mac下:pycharm 编辑器有print的功能,但是会提示: Error: No print service found. 所以需要一个 ...
- Python Thrift 简单示例
本文基于Thrift-0.10,使用Python实现服务器端,使用Java实现客户端,演示了Thrift RPC调用示例.Java客户端提供两个字符串参数,Python服务器端计算这两个字符串的相似度 ...
- python psutil简单示例
python psutil简单示例 利用psutil编写简单的检测小脚本 0.安装psutil模块 ...
- python 日志打印之logging使用介绍
python 日志打印之logging使用介绍 by:授客QQ:1033553122 测试环境: Python版本:Python 2.7 简单的将日志打印到屏幕 import logging lo ...
- Python操作SQLServer示例(转)
转自:http://www.cnblogs.com/lrzy/p/4346781.html 本文主要是Python操作SQLServer示例,包括执行查询及更新操作(写入中文). 需要注意的是:读取数 ...
- 转:Python操作SQLServer示例
注:此文也是转载,2018年1月发现此文阅读量过万,略感不安.当时只是为了自己存档学习,未粘此文的原始连接.如有侵权,通过即删除,敬请谅解! 从网上找的,估计原文是:Python操作SQLServer ...
- Websocket - Websocket原理(握手、解密、加密)、基于Python实现简单示例
一.Websocket原理(握手.解密.加密) WebSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符.它实 ...
- Python操作SQLServer示例
本文主要是Python操作SQLServer示例,包括执行查询及更新操作(写入中文). 需要注意的是:读取数据的时候需要decode('utf-8'),写数据的时候需要encode('utf-8'), ...
- Python实现打印螺旋矩阵功能的方法
Python实现打印螺旋矩阵功能的方法 本文实例讲述了Python实现打印螺旋矩阵功能的方法.分享给大家供大家参考,具体如下: 一.问题描述 输入N, 打印 N*N 螺旋矩阵 比如 N = 3,打印: ...
随机推荐
- CentOS7修改网卡为eth0
CentOS7修改网卡为eth0 1.编辑网卡信息 [root@linux-node2~]# cd /etc/sysconfig/network-scripts/ #进入网卡目录 [root@lin ...
- libz.so库分析
from:http://blog.chinaunix.net/uid-12773189-id-84605.html 1.查看库文件是由哪个软件包提供的空闲时打开/usr/lib目录(因为我知道这个目录 ...
- 使用WebBrowser的记录
第一:新建一个类,用了获取WebBrowser元素的类 //需要引用 Interop.SHDocVw 和 Microsoft.mshtmlpublic class Element { //根据Name ...
- 在Linux下不使用密码远程登陆其他Linux
有时需要再一台Linux上登陆其他Linux服务器,通常可以直接使用SSH命令,加入两台服务器一台服务器A,IP地址192.168.1.2,另一台服务器B,IP地址192.168.1.3,如果想从A服 ...
- 互联网HTTP连接等出错代码大全
100 - Continue 101 - Switching Protocols Success Codes 200 - OK 201 - Created 202 - Accepted 20 ...
- java中抽象类与接口的区别
1.abstract class 在 Java 语言中表示的是一种继承关系,一个类只能使用一次继承关系.但是,一个类却可以实现多个interface. 2.在abstract class 中可以有自己 ...
- 简单的介绍下WPF中的MVVM框架
最近在研究学习Swift,苹果希望它迅速取代复杂的Objective-C开发,引发了一大堆热潮去学它,放眼望去各个培训机构都已打着Swift开发0基础快速上手的招牌了.不过我觉得,等同于无C++基础上 ...
- MVC + JQUERY + AJAX
- HDU1004 (数组元素出现最多)
HDU1004 思路:求数组中出现次数最多的那个元素: 遍历数组元素,找出每个元素出现的次数 Input Input contains multiple test cases. Each test c ...
- 《C和指针》章节后编程练习解答参考——6.3
<C和指针>——6.3 题目: 编写一个函数,把参数字符串中的字符反向排列. 函数原型: void reverse_string(char *string); 要求: 使用指针而不是数组下 ...