Python27 - on windows 10 how can i tell printing paper size is 50.8mm x 25.4mm?

五迷三道 提交于 2021-02-16 20:58:07

问题


I have this code on windows which is working with A4 printers. But i have a very small printer, when i print something it does not print anything but only push the ticket out.

TRY0:

import win32ui
X=0; Y=0
input_string = "Print 1234 test"
multi_line_string = input_string.split()
hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC ('Dymo label printer')
hDC.StartDoc ('FILE NAME FILE NAME')
hDC.StartPage ()
for line in multi_line_string:
     hDC.TextOut(X,Y,line)
     #Y += 100
hDC.EndPage ()
hDC.EndDoc ()

TRY1: faling to print same like before: https://msdn.microsoft.com/en-us/library/windows/desktop/dd319099(v=vs.85).aspx

import win32ui
import win32con
import win32print
import win32gui

X=0
Y=0
input_string = "A390"

printer = win32print.GetDefaultPrinter()
hprinter = win32print.OpenPrinter(printer)
devmode = win32print.GetPrinter(hprinter, 2)["pDevMode"]

# change paper size and orientation
# constants are available here:
# http://msdn.microsoft.com/library/default.asp?
#      url=/library/en-us/intl/nls_Paper_Sizes.asp
# number 10 envelope is 20
devmode.PaperSize = 20
# 1 = portrait, 2 = landscape
devmode.Orientation = 1

hdc = win32gui.CreateDC("WINSPOOL", printer, devmode)
dc = win32ui.CreateDCFromHandle(hdc)
dc.StartDoc ('test')
dc.StartPage ()
dc.TextOut(X,Y,input_string)
#for line in multi_line_string:
     #hDC.TextOut(X,Y,line)
dc.EndPage ()
dc.EndDoc ()

TRY2: FAIL

import win32ui
import win32con
import win32print
import win32gui

printer = win32print.GetDefaultPrinter()
hprinter = win32print.OpenPrinter(printer)
devmode = win32print.GetPrinter(hprinter, 2)["pDevMode"]
devmode.PaperSize = 20
devmode.Orientation = 2

hDC = win32gui.CreateDC ("WINSPOOL", printer, devmode)
dc = win32ui.CreateDCFromHandle(hDC)
dc.StartDoc ('test')
dc.StartPage ()

#hDC.SetMapMode(MM_TEXT) # ERROR no MM_TEXT found...
font = win32ui.CreateFont({
    "name": "Lucida Console",
    "height": 50,
    "weight": 50,
})
win32print.DocumentProperties(0, hprinter, '300LN1', None, None, 5)
dc.SelectObject(font)
dc.TextOut(10,10,"TEST...")
dc.TextOut(10,-10,"TEST..")

dc.EndPage ()
dc.EndDoc ()

TRY3: FAIL

import win32ui
import win32print
import win32con

INCH = 1440

hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC (win32print.GetDefaultPrinter ())
hDC.StartDoc ("Test doc")
hDC.StartPage ()
hDC.SetMapMode (win32con.MM_TWIPS)
hDC.DrawText ("TEST", (0, INCH * -1, INCH * 8, INCH * -2), win32con.DT_CENTER)
hDC.EndPage ()
hDC.EndDoc ()

回答1:


You should try this:

#MM_TEXT= Each logical unit is mapped to one device pixel. Positive x is to the right; positive y is down. OR to draw MM_TWIPS see link
hDC.SetMapMode(MM_TEXT)
font = win32ui.CreateFont({
    "name": "Lucida Console",
    "height": 10,
    "weight": 20,
})
win32print.DocumentProperties(0, pHandle, '300LN1', None, None, 5)
hDC.SelectObject(font)
#First print this test to know if it is inverted as user say it
# Also don't forget to keep **LONG TEXT TEST** to have chance to see it if not well placed.
hDC.TextOut(10,10,"This is a Test/ This is a second test...................")
hDC.TextOut(10,-10,"This is the bottom Test/ This is a second bottom test.................")

From:

http://newcenturycomputers.net/projects/pythonicwindowsprinting.html

https://msdn.microsoft.com/fr-fr/library/windows/desktop/dd162980(v=vs.85).aspx



来源:https://stackoverflow.com/questions/38178454/python27-on-windows-10-how-can-i-tell-printing-paper-size-is-50-8mm-x-25-4mm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!