Masood Masood - 6 months ago 33
Linux Question

Windows Python Code not working on Linux Debian

The code generates a QR code and prints it, but It is not working on the Debian Os due to not supporting the imported libraries (win32print, Win32ui).
Can anyone tell me how to run it on the Debian without changing the whole code.




from random import randint
import win32print
import win32ui

from PIL import Image, ImageWin
from PIL._imaging import font
from PIL import ImageFont
from PIL import ImageDraw



HORZRES = 8
VERTRES = 10

LOGPIXELSX = 88
LOGPIXELSY = 90

PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111


PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113


__author__ = 'masoodhussain'

import qrcode
import subprocess
import os

qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)

qr.add_data('Masooddkjfdlfs,kokdfds sddshfhkjshfljsdhkjfdrtyyhtfhfghgh3')
qr.make(fit=True)

"subprocess.call(['lp', 'foo.png'])"

printer_name = win32print.GetDefaultPrinter()

img = qr.make_image()

img.show()

random_number= randint(0,10000)

img.save('label_'+str(random_number)+'.png')

file_name = 'label_'+str(random_number)+'.png'

print(file_name)


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)


bmp = Image.open (file_name)


if bmp.size[0] > bmp.size[1]:
bmp = bmp.rotate (90)

ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
scale = min (ratios)


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) / 2)
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 ()


when I run the code by removing the unsupported libraries it gives an error on this part: error importing

import qrcode


I am trying to import whole folder for using there other files. In Windows it was working perfectly. Any help would be appreciated.Thanks

Answer

This code is equivalent to the code posted in Question.

from random import randint
import cups

from PIL import Image, ImageWin
from PIL._imaging import font
from PIL import ImageFont
from PIL import ImageDraw


__author__ = 'masoodhussain'

import qrcode

qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=5,
    border=2,
)

qr.add_data('localhost:5070productinfo')
qr.make(fit=True)

conn= cups.Connection()
printer_name = conn.getPrinters()

printer_name = printer_name.keys()[0]

printqueuelength = len(conn.getJobs())

img = qr.make_image()

img.show()

random_number= randint(0,10000)

img.save('label_'+str(random_number)+'.png')

file_name = 'label_'+str(random_number)+'.png'

print(file_name)

conn.printFile(printer_name,file_name,"Hello", options ={'media':'25x25mm'}) 

Important part is the installation of required libraries and changing your media to the required size.