Python Win32 Print Sample

Source: Internet
Author: User

1 #-*-coding:utf-8-*-2 #author:pete yim<[email protected]>3 #date:13-8-224 #Copyright (c) rcsh Systems Incorporated. All rights reserved.5 ImportWin32print6 ImportWin32ui7  fromPILImportImage, Imagewin8 Import_imaging9 Ten # One #Constants for GetDeviceCaps A # - # - #horzres/vertres = printable area the # -Horzres = 8 -Vertres = 10 - # + #logpixels = dots per inch - # +LOGPIXELSX = 88 ALogpixelsy = 90 at # - #physicalwidth/height = total area - # -Physicalwidth = 110 -Physicalheight = 111 - # in #physicaloffsetx/y = left/top margin - # toPhysicaloffsetx = 112 +Physicaloffsety = 113 -  thePrinter_name =Win32print. GetDefaultPrinter () *file_name ="e:\\abc.jpg" $ Panax Notoginseng # - #can only write a device-independent bitmap the #directly to a Windows device context; therefore + #we need (for ease) to use the Python Imaging A #Library to manipulate the image. the # + #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) thePrinter_size =Hdc.getdevicecaps (physicalwidth), Hdc.getdevicecaps (physicalheight) -Printer_margins =Hdc.getdevicecaps (PHYSICALOFFSETX), Hdc.getdevicecaps (physicaloffsety)Wuyi  the # - #Open the image, rotate it if it ' s wider than Wu #It is-high, and work-out how-much to multiply - #Each pixel-to- get it as big as possible on About #The page without distorting. $ # -BMP =Image.open (file_name) - ifBmp.size[0] > Bmp.size[1]: -BMP = Bmp.rotate (90) A  +ratios = [1.0 * printable_area[0]/bmp.size[0], 1.0 * printable_area[1]/bmp.size[1]] theScale =min (ratios) -  $ # the #Start The print job, and draw the bitmap to the #The printer device at the scaled size. the # the Hdc.startdoc (file_name) - hdc.startpage () in  theDIB =Imagewin.dib (BMP) theScaled_width, scaled_height = [Int (scale * i) forIinchBmp.size] Aboutx1 = Int ((printer_size[0]-scaled_width)/2) they1 = Int ((printer_size[1]-scaled_height)/2) thex2 = x1 +Scaled_width they2 = y1 +Scaled_height + Dib.draw (Hdc.gethandleoutput (), (x1, y1, x2, y2)) -  the hdc.endpage ()Bayi Hdc.enddoc () theHdc.deletedc ()

Note: Python calls the Win32 interface to print photos

Python Win32 Print Sample

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.