Iseries Java Toolbox Read Afp Printer File
python connect the printer to print documents, images, pdf files, etc.
tags: python printer win32print Print Picture PIL
introduction
python continued to the printer for printing, may vary according to dissimilar needs, a different function modules.
- If you simply want to print a document, such every bit office documents, yous can apply
ShellExecute
Methods for Microsoft office documents, pdf, txt and so helpful, you tin attempt; - If y'all enter some data, text messages, wanted to send it direct to the printer, you can effort to employ
win32print
; - If yous have a picture, yous tin combine the python
Python Imaging Library(PIL)
withwin32ui
Module for printing;
Ordinary print
ShellExecute
- First make sure you use your calculator can open up the file y'all want to print;
- Are some of the standard file types
- Do not control which printers, and printer model that is contained of the connexion;
- You lot gear up the impress properties uncontrolled authorisation;
import tempfile import win32api import win32print filename = tempfile.mktemp ( ".txt" ) open (filename, "w" ) .write ( "This is a test" ) win32api.ShellExecute ( 0 , "print" , filename, # # If this is None, the default printer will # be used anyway. # '/d:"%due south"' % win32print.GetDefaultPrinter ( ) , "." , 0 )
Another version
import tempfile import win32api import win32print filename = tempfile.mktemp ( ".txt" ) open (filename, "westward" ) .write ( "This is a exam" ) win32api.ShellExecute ( 0 , "printto" , filename, '"%southward"' % win32print.GetDefaultPrinter ( ) , "." , 0 )
Direct print data
win32print
- Throw the printer direct to the information;
- Quick and easy;
- And you can choose which printer is defined;
- However, data to be printed must be printed, for instance, string and the like;
import bone, sys import win32print printer_name = win32print.GetDefaultPrinter ( ) # # raw_data could as be raw PCL/PS read from # some impress-to-file operation # if sys.version_info >= ( iii , ) : raw_data = bytes ( "This is a test" , "utf-viii" ) else : raw_data = "This is a test" hPrinter = win32print.OpenPrinter (printer_name) try : hJob = win32print.StartDocPrinter (hPrinter, ane , ( "test of raw data" , None , "RAW" ) ) endeavor : win32print.StartPagePrinter (hPrinter) win32print.WritePrinter (hPrinter, raw_data) win32print.EndPagePrinter (hPrinter) finally : win32print.EndDocPrinter (hPrinter) finally : win32print.ClosePrinter (hPrinter)
Print Picture show
PIL win32ui
Without the use of additional tools, impress a motion-picture show on a windows calculator is quite hard and requires at least 3 unlike and related equipment environment tin.
Fortunately,device-independent bitmap(DIB)
withPIL
It can help u.s.a. to quickly impress. The post-obit code can exist sent to the printer to impress images as large as possible without losing the size and proportion.
- You can as well choose which printer
- Select loaded Picture format
- Just if you're reckoner is not windows, information technology may not be the best approach;
import win32print import win32ui from PIL import Image, ImageWin # # Constants for GetDeviceCaps # # # HORZRES / VERTRES = printable expanse # HORZRES = 8 VERTRES = 10 # # LOGPIXELS = dots per inch # LOGPIXELSX = 88 LOGPIXELSY = ninety # # PHYSICALWIDTH/Superlative = total area # PHYSICALWIDTH = 110 PHYSICALHEIGHT = 111 # # PHYSICALOFFSETX/Y = left / top margin # PHYSICALOFFSETX = 112 PHYSICALOFFSETY = 113 printer_name = win32print.GetDefaultPrinter ( ) file_name = "test.jpg" # # You can only write a Device-independent bitmap # directly to a Windows device context; therefore # we need (for ease) to utilize 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 paradigm, rotate it if it'south wider than # it is high, and piece of work out how much to multiply # each pixel by to go information technology as big as possible on # the page without distorting. # bmp = Paradigm. open (file_name) if bmp.size[ 0 ] > bmp.size[ ane ] : bmp = bmp.rotate ( 90 ) ratios = [ ane.0 * printable_area[ 0 ] / bmp.size[ 0 ] , one.0 * printable_area[ i ] / bmp.size[ 1 ] ] calibration = min (ratios) # # Starting time 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) / two ) 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 ( )
Above game for two Jun finishing release, delight indicate the source, thank you.
reference
http://timgolden.me.uk/python/win32_how_do_i/print.htm
Intelligent Recommendation
Printshare connect printer, print word
After getting a week, finally finally. Our company is doing face recognition technology recently. After the recognition, yous need to impress the give-and-take (printShare implementation). Let's see how to achiev...
More Recommendation
Python printer print sequence
Some printers do not back up automated two-sided printing, and you can specify the printing sequence yourself when manually press on both sides. Opposite printing rules: empty side up, page number o...
Copyright DMCA © 2018-2022 - All Rights Reserved - www.programmersought.com User Find
mitchellthavorn1969.blogspot.com
Source: https://programmersought.com/article/76882776470/
0 Response to "Iseries Java Toolbox Read Afp Printer File"
Postar um comentário