Guten Tag. Habe eine Frage, weil sonst bin schon seit einem Monat dran.
Habe mein kleines Programmchen schon soweit fertig. Für den Anfang etwas zuviel vorgenommen aber fast alles geschafft.
Es ging um ein Formular den man ausfüllt und das ganze wird in einer Tabelle.ods gespeichert.
Nun wollte ich auch das man nach dem speichern es auch drucken tut. Weil es jedes mal ca. 5 Blätter sind, wollte ich es automatisch machen
lassen über Python. Nun müsste ich feststellen das im Jahr 2021 keiner wirklich über Python drucken möchte.
http://timgolden.me.uk/python/win32_how_do_i/print.html
Das ist sehr komisch beschrieben für mich und größe A6 ist nicht dabei.
Habe versucht Windows 10 Editor die Größe auf A6 voreinzustellen, aber im Jahr 2021 für Editor etwas zu ändern ist nicht.
https://www.pdftron.com/documentation/s ... FPrintTest
Hier sah etwas nach einer Lösung aus aber nur bis Python Version 3.8
Python.os hab auch versucht.
Habe bei Youtube möchtegern Profis gefragt, sehe nur immer Copy/Past Inhalt und keiner meldet sich.
Deswegen dachte mal hier nachzufragen.
Wenn nichts hilft dann werde mich auf C,C++ umsatteln und diese 2 Zeilen damit schreiben, auch wenn es wieder ein Monat dauern wird.
Danke im Voraus
Python Print Hard Copy A6 Size / Mit Python Drucken Din A6 Größe
-
- User
- Beiträge: 4
- Registriert: Dienstag 6. Juli 2021, 11:02
Danke für die Antwort erstmal. Subprocess.run als Modul keine Dokumentation. Dazu:
Supported platforms
Python2.6
Python2.7
Python3.3
PyPy2.1
warscheinlich wird auch nicht unterstützt.
Beim Modul Subprocess ist auch echt komische Dokumentation.
https://docs.python.org/3/library/subprocess.html
Als Leihe kann ich mir solche Beschreibungen gleich sparen.
Der beste Vorschlag war bei Youtube. Man soll mit pyautogui Strg + P und Enter simulieren
Supported platforms
Python2.6
Python2.7
Python3.3
PyPy2.1
warscheinlich wird auch nicht unterstützt.
Beim Modul Subprocess ist auch echt komische Dokumentation.
https://docs.python.org/3/library/subprocess.html
Als Leihe kann ich mir solche Beschreibungen gleich sparen.
Der beste Vorschlag war bei Youtube. Man soll mit pyautogui Strg + P und Enter simulieren

-
- User
- Beiträge: 4
- Registriert: Dienstag 6. Juli 2021, 11:02
So jetzt hab ich es endlich. Einfach nur eine Zeile..
import win32print, win32con
import win32ui, win32gui
from PIL import Image, ImageWin
#
# Constants for GetDeviceCaps
#
#
# HORZRES / VERTRES = printable area
#
HORZRES = 5
VERTRES = 7
#
# LOGPIXELS = dots per inch
#
LOGPIXELSX = 288
LOGPIXELSY = 290
#
# PHYSICALWIDTH/HEIGHT = total area
#
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
#
# PHYSICALOFFSETX/Y = left / top margin
#
PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113
printer_name = win32print.GetDefaultPrinter ()
file_name = "cheque_image.bmp"
#
# You can only write a Device-independent bitmap
# directly to a Windows device context; therefore
# we need (for ease) to use 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 ()
printer = win32print.GetDefaultPrinter()
hprinter = win32print.OpenPrinter(printer)
devmode = win32print.GetPrinter(hprinter, 2)["pDevMode"]
devmode.PaperSize = win32con.DMPAPER_A4 ###Genau das hat mir gefehlt
# 1 = portrait, 2 = landscape ###Hier noch die Größen https://docs.microsoft.com/en-us/window ... aper-sizes
devmode.Orientation = 1
# create dc using new settings.
# first get the integer hDC value. note that we need the name.
hdc = win32gui.CreateDC("WINSPOOL", printer, devmode)
# next create a PyCDC from the hDC.
dc = win32ui.CreateDCFromHandle(hdc)
dc.CreatePrinterDC()
# dc.StartDoc(file_name)
# dc.StartPage()
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 image, rotate it if it's wider than
# it is high, and work out how much to multiply
# each pixel by to get it as big as possible on
# the page without distorting.
#
bmp = Image.open (file_name)
# if bmp.size[0] > bmp.size[1]:
# bmp = bmp.rotate (270)
#
# ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
# scale = 2
bmp = bmp.rotate (270)
#
# Start the print job, and draw the bitmap to
# the printer device at the scaled size.
#
hDC.StartDoc (file_name)
hDC.StartPage ()
scale = 1.5
dib = ImageWin.Dib(bmp)
scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
# x1 = int ((printer_size[0] - scaled_width) / 2)
# y1 = int ((printer_size[1] - scaled_height) / 2)
# y1 = -110
# x1 = 1530
# x2 = x1 + scaled_width
# y2 = y1 + scaled_height
# import pdb; pdb.set_trace()
dib.draw(hDC.GetHandleOutput(), (988, -111, scaled_width+988,scaled_height-111 ))
hDC.EndPage ()
hDC.EndDoc ()
hDC.DeleteDC ()
# dc.EndPage()
# dc.EndDoc()
# dc.DeleteDC()
import win32print, win32con
import win32ui, win32gui
from PIL import Image, ImageWin
#
# Constants for GetDeviceCaps
#
#
# HORZRES / VERTRES = printable area
#
HORZRES = 5
VERTRES = 7
#
# LOGPIXELS = dots per inch
#
LOGPIXELSX = 288
LOGPIXELSY = 290
#
# PHYSICALWIDTH/HEIGHT = total area
#
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
#
# PHYSICALOFFSETX/Y = left / top margin
#
PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113
printer_name = win32print.GetDefaultPrinter ()
file_name = "cheque_image.bmp"
#
# You can only write a Device-independent bitmap
# directly to a Windows device context; therefore
# we need (for ease) to use 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 ()
printer = win32print.GetDefaultPrinter()
hprinter = win32print.OpenPrinter(printer)
devmode = win32print.GetPrinter(hprinter, 2)["pDevMode"]
devmode.PaperSize = win32con.DMPAPER_A4 ###Genau das hat mir gefehlt
# 1 = portrait, 2 = landscape ###Hier noch die Größen https://docs.microsoft.com/en-us/window ... aper-sizes
devmode.Orientation = 1
# create dc using new settings.
# first get the integer hDC value. note that we need the name.
hdc = win32gui.CreateDC("WINSPOOL", printer, devmode)
# next create a PyCDC from the hDC.
dc = win32ui.CreateDCFromHandle(hdc)
dc.CreatePrinterDC()
# dc.StartDoc(file_name)
# dc.StartPage()
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 image, rotate it if it's wider than
# it is high, and work out how much to multiply
# each pixel by to get it as big as possible on
# the page without distorting.
#
bmp = Image.open (file_name)
# if bmp.size[0] > bmp.size[1]:
# bmp = bmp.rotate (270)
#
# ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
# scale = 2
bmp = bmp.rotate (270)
#
# Start the print job, and draw the bitmap to
# the printer device at the scaled size.
#
hDC.StartDoc (file_name)
hDC.StartPage ()
scale = 1.5
dib = ImageWin.Dib(bmp)
scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
# x1 = int ((printer_size[0] - scaled_width) / 2)
# y1 = int ((printer_size[1] - scaled_height) / 2)
# y1 = -110
# x1 = 1530
# x2 = x1 + scaled_width
# y2 = y1 + scaled_height
# import pdb; pdb.set_trace()
dib.draw(hDC.GetHandleOutput(), (988, -111, scaled_width+988,scaled_height-111 ))
hDC.EndPage ()
hDC.EndDoc ()
hDC.DeleteDC ()
# dc.EndPage()
# dc.EndDoc()
# dc.DeleteDC()