Python win32打印示例

 1 # -*- coding:utf-8 -*-
 2 # Author: Pete Yim<[email protected]>
 3 # Date  : 13-8-22
 4 # Copyright (c) 2013 RCSH Systems Incorporated. All rights reserved.
 5 import win32print
 6 import win32ui
 7 from PIL import Image, ImageWin
 8 import _imaging
 9
10 #
11 # Constants for GetDeviceCaps
12 #
13 #
14 # HORZRES / VERTRES = printable area
15 #
16 HORZRES = 8
17 VERTRES = 10
18 #
19 # LOGPIXELS = dots per inch
20 #
21 LOGPIXELSX = 88
22 LOGPIXELSY = 90
23 #
24 # PHYSICALWIDTH/HEIGHT = total area
25 #
26 PHYSICALWIDTH = 110
27 PHYSICALHEIGHT = 111
28 #
29 # PHYSICALOFFSETX/Y = left / top margin
30 #
31 PHYSICALOFFSETX = 112
32 PHYSICALOFFSETY = 113
33
34 printer_name = win32print.GetDefaultPrinter ()
35 file_name = "E:\\abc.jpg"
36
37 #
38 # You can only write a Device-independent bitmap
39 #  directly to a Windows device context; therefore
40 #  we need (for ease) to use the Python Imaging
41 #  Library to manipulate the image.
42 #
43 # Create a device context from a named printer
44 #  and assess the printable size of the paper.
45 #
46 hDC = win32ui.CreateDC ()
47 hDC.CreatePrinterDC (printer_name)
48 printable_area = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps (VERTRES)
49 printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT)
50 printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY)
51
52 #
53 # Open the image, rotate it if it‘s wider than
54 #  it is high, and work out how much to multiply
55 #  each pixel by to get it as big as possible on
56 #  the page without distorting.
57 #
58 bmp = Image.open (file_name)
59 if bmp.size[0] > bmp.size[1]:
60   bmp = bmp.rotate (90)
61
62 ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
63 scale = min (ratios)
64
65 #
66 # Start the print job, and draw the bitmap to
67 #  the printer device at the scaled size.
68 #
69 hDC.StartDoc (file_name)
70 hDC.StartPage ()
71
72 dib = ImageWin.Dib (bmp)
73 scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
74 x1 = int ((printer_size[0] - scaled_width) / 2)
75 y1 = int ((printer_size[1] - scaled_height) / 2)
76 x2 = x1 + scaled_width
77 y2 = y1 + scaled_height
78 dib.draw (hDC.GetHandleOutput (), (x1, y1, x2, y2))
79
80 hDC.EndPage ()
81 hDC.EndDoc ()
82 hDC.DeleteDC ()

注:python调用win32接口打印照片

时间: 2024-10-06 00:07:08

Python win32打印示例的相关文章

python合并文本文件示例代码

python合并文本文件示例代码. python实现两个文本合并employee文件中记录了工号和姓名cat employee.txt: 100 Jason Smith200 John Doe300 Sanjay Gupta400 Ashok Sharma bonus文件中记录工号和工资cat bonus.txt: 100 $5,000200 $500300 $3,000400 $1,250要求把两个文件合并并输出如下, 处理结果:400 ashok sharma $1,250 100 jaso

Python操作SQLServer示例

本文主要是Python操作SQLServer示例,包括执行查询及更新操作(写入中文). 需要注意的是:读取数据的时候需要decode('utf-8'),写数据的时候需要encode('utf-8'),这样就可以避免烦人的中文乱码或报错问题. Python操作SQLServer需要使用pymssql模块,使用pip install pymssql安装即可. 此外代码中使用的封装MSSQL类是从网上搜索到的,直接用即可. # -*- coding:utf-8 -*- import pymssql c

Python 3.X 调用多线程C模块,并在C模块中回调python函数的示例

由于最近在做一个C++面向Python的API封装项目,因此需要用到C扩展Python的相关知识.在此进行简要的总结. 此篇示例分为三部分.第一部分展示了如何用C在Windows中进行多线程编程:第二部分将第一部分的示例进行扩展,展示了如何在python中调用多线程的C模块:第三部分扩展了第二部分,增加了在C模块的线程中回调python的演示. 本文所用的环境为:64位Win7 + python 3.4 x86 + vs2010 一.windows下的C语言多线程程序 windows下多线程编程

python win32 简单操作

源由 刚开始是帮朋友做一个按键精灵操作旺信的脚本,写完后各种不稳定:后来看到python可以操作win32相关的api,恰好这一段时间正在学习python,感觉练手的时候到了~~~ 下载 要注意Python版本及位数,否则会安装失败 直接到上面的地址去找合适的版本下载安装,已包含其它的工具 下载的已经是可执行文件,直接执行即可 https://sourceforge.net/projects/pywin32/ 获取句柄的方式 VC或VS工具里面自带SPY++,可以获取句柄信息, 这个你没有,请看

Python实现打印二叉树某一层的所有节点

不多说,直接贴程序,如下所示 # -*- coding: utf-8 -*- # 定义二叉树节点类 class TreeNode(object): def __init__(self,data=0,left=0,right=0): self.data = data self.left = left self.right = right # 遍历某一层所有节点,并打印 def TransLevel(root,level): if root == None: return else: if leve

Python:打印某个路径下的所有文件

打印某个路径下的所有文件,我们可以通过find命令实现(find 路径 -type f).下面我通过 Python 的递归来实现这个功能. [[email protected] ~]# vim print_files.py #!/usr/bin/python import os import sys def print_files(path): lsdir = os.listdir(path) dirs = [i for i in lsdir if os.path.isdir(os.path.j

boost.python编译及示例

欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/46781581 linux编译boost的链接:http://blog.csdn.net/majianfei1023/article/details/46761029 昨天编译安装好boost,今天准备使用boost.python写个python调用c++代码的例子,结果踩了很多坑. 首先贴上代码: 1.student.cpp,一个普通的c++类 #include

Python 字符串格式化示例

先看代码吧. #!/usr/bin/env python #-*- coding: utf-8 -*- __author__ = 'jiang' __creattime__ = '2015/10/31 23:40' width = input('please enter width:') price_width = 10 item_width = width - price_width header_format = '%-*s%*s' format = '%-*s%*.2f' print '=

Python:打印目录下最大的十个文件

打印指定目录下最大的十个文件,并按倒序输出: vim top10.py #!/usr/bin/env python import os import sys import operator def gen_dic(topdir): dic = {} a = os.walk(topdir) for p, d, f in a: for i in f: fn = os.path.join(p, i) f_size = os.path.getsize(fn) dic[fn] = f_size retur