1. # -*- coding:utf-8 -*-
  2. # Author: Pete Yim<xpHook@gmail.com>
  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接口打印照片

Python win32打印示例的更多相关文章

  1. 将python代码打印成pdf

    将python代码打印成pdf,打印出来很丑,完全不能看. mac下:pycharm 编辑器有print的功能,但是会提示: Error: No print service found. 所以需要一个 ...

  2. Python Thrift 简单示例

    本文基于Thrift-0.10,使用Python实现服务器端,使用Java实现客户端,演示了Thrift RPC调用示例.Java客户端提供两个字符串参数,Python服务器端计算这两个字符串的相似度 ...

  3. python psutil简单示例

    python psutil简单示例 利用psutil编写简单的检测小脚本 0.安装psutil模块                                                    ...

  4. python 日志打印之logging使用介绍

    python 日志打印之logging使用介绍 by:授客QQ:1033553122 测试环境: Python版本:Python 2.7   简单的将日志打印到屏幕 import logging lo ...

  5. Python操作SQLServer示例(转)

    转自:http://www.cnblogs.com/lrzy/p/4346781.html 本文主要是Python操作SQLServer示例,包括执行查询及更新操作(写入中文). 需要注意的是:读取数 ...

  6. 转:Python操作SQLServer示例

    注:此文也是转载,2018年1月发现此文阅读量过万,略感不安.当时只是为了自己存档学习,未粘此文的原始连接.如有侵权,通过即删除,敬请谅解! 从网上找的,估计原文是:Python操作SQLServer ...

  7. Websocket - Websocket原理(握手、解密、加密)、基于Python实现简单示例

    一.Websocket原理(握手.解密.加密) WebSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符.它实 ...

  8. Python操作SQLServer示例

    本文主要是Python操作SQLServer示例,包括执行查询及更新操作(写入中文). 需要注意的是:读取数据的时候需要decode('utf-8'),写数据的时候需要encode('utf-8'), ...

  9. Python实现打印螺旋矩阵功能的方法

    Python实现打印螺旋矩阵功能的方法 本文实例讲述了Python实现打印螺旋矩阵功能的方法.分享给大家供大家参考,具体如下: 一.问题描述 输入N, 打印 N*N 螺旋矩阵 比如 N = 3,打印: ...

随机推荐

  1. CentOS7修改网卡为eth0

    CentOS7修改网卡为eth0 1.编辑网卡信息 [root@linux-node2~]# cd /etc/sysconfig/network-scripts/  #进入网卡目录 [root@lin ...

  2. libz.so库分析

    from:http://blog.chinaunix.net/uid-12773189-id-84605.html 1.查看库文件是由哪个软件包提供的空闲时打开/usr/lib目录(因为我知道这个目录 ...

  3. 使用WebBrowser的记录

    第一:新建一个类,用了获取WebBrowser元素的类 //需要引用 Interop.SHDocVw 和 Microsoft.mshtmlpublic class Element { //根据Name ...

  4. 在Linux下不使用密码远程登陆其他Linux

    有时需要再一台Linux上登陆其他Linux服务器,通常可以直接使用SSH命令,加入两台服务器一台服务器A,IP地址192.168.1.2,另一台服务器B,IP地址192.168.1.3,如果想从A服 ...

  5. 互联网HTTP连接等出错代码大全

    100 - Continue  101 - Switching Protocols Success Codes  200 - OK  201 - Created  202 - Accepted  20 ...

  6. java中抽象类与接口的区别

    1.abstract class 在 Java 语言中表示的是一种继承关系,一个类只能使用一次继承关系.但是,一个类却可以实现多个interface. 2.在abstract class 中可以有自己 ...

  7. 简单的介绍下WPF中的MVVM框架

    最近在研究学习Swift,苹果希望它迅速取代复杂的Objective-C开发,引发了一大堆热潮去学它,放眼望去各个培训机构都已打着Swift开发0基础快速上手的招牌了.不过我觉得,等同于无C++基础上 ...

  8. MVC + JQUERY + AJAX

  9. HDU1004 (数组元素出现最多)

    HDU1004 思路:求数组中出现次数最多的那个元素: 遍历数组元素,找出每个元素出现的次数 Input Input contains multiple test cases. Each test c ...

  10. 《C和指针》章节后编程练习解答参考——6.3

    <C和指针>——6.3 题目: 编写一个函数,把参数字符串中的字符反向排列. 函数原型: void reverse_string(char *string); 要求: 使用指针而不是数组下 ...