使用win32print + cx_Freeze时,如果不产生任何错误,打印指令将无法正常工作

时间:2019-01-17 15:37:14

标签: python winapi cx-freeze

Windows 10(x64) Python 3.6.3 cx_Freeze 5.1.1 pypiwin32 223 / pywin32 224

我制作了一个用于打印的模块,该模块作为脚本启动时可以正常工作。 一旦通过cx_Freeze,print命令将在不产生任何错误消息的情况下不起作用。 这是我的setup.py用于创建版本(作者:python setup.py build

# -*- coding: utf-8 -*-
import sys,os
from cx_Freeze import setup, Executable

PythonPath = os.path.split(sys.executable)[0] #get python path

includes = []

excludes = []

packages = ["win32print"]

includefiles = ["OptiWeb.ico"]

options = {"includes": includes,
           "excludes": excludes,
           "packages": packages,
           "include_files": includefiles,
           "optimize":2
           }

base=None

if sys.platform == 'win32':
    base = "Win32GUI"
    options["include_msvcr"] = True


executables = [Executable(script="OptiPrint.py",base=base,icon="Optiweb.ico")]

setup(name = "OptiPrint",options = {"build_exe": options},version = "1.0",author = "ND",description = "print test",executables = executables)

现在是我的打印代码:

# coding: utf8

import win32print

class ZPLLabel(object):
    def __init__(self, printerName):
        self.printerName = printerName
        self.printerDevice = win32print.OpenPrinter(self.printerName)
        self.job = win32print.StartDocPrinter(self.printerDevice, 1, ("Etiquette", None, "RAW"))
        self.eraseAll()
        self.defineFormat()

    def eraseAll(self):
        win32print.StartPagePrinter(self.printerDevice)
        str2print="~JA"
        win32print.WritePrinter(self.printerDevice, str2print.encode("utf8")) #écrit le format d'étiquette
        win32print.EndPagePrinter(self.printerDevice) # indique la fin de ce qu'il y a à imprimer
        self.printerDevice.close() # ferme le canal d'impression et déclenche l'impression de ce qui précède
        #del self.job    
        self.printerDevice=win32print.OpenPrinter(self.printerName)
        self.job = win32print.StartDocPrinter(self.printerDevice, 1, ("Etiquette", None, "RAW"))

    def defineFormat(self):
        margeLeft = 150
        margeTop = 20
        interLine = 39
        shiftLeft = 20
        vDec = 25
        #win32print.StartPagePrinter(p)
        str2print="^XA\n" #debut de format
        str2print+="^CI28"
        #FO origine du champ, 100 pos x du champ en dots, 50 pos y du champ en dots
        # l'imprimantes est 200 dpi (dotsper inch = 7.874 dots par mm, ici 12.7mm, 6.35mm)
        #ADN : A ==> font, D==> font D, N ==> Orientation Normale, 36 hauteur caractère en dots, 20 Largeur caractère en dots
        #FD données à imprimer pour le champ
        #FS fin du champ
        str2print+="^DFFORMAT"
        str2print+="^LH"+str(margeLeft)+","+str(margeTop)
        #un cadre arrondi
        str2print+="^FO0,0^GB500,330,3,B,2^FS"    
        #str2print+="^FO"+str(shiftLeft)+","+str(interLine)+"^ADN,24,12^FDEtiquette de débit Sangle^FS\n" #format de l'étiquette
        str2print+="^FO"+str(shiftLeft)+","+str(1*interLine-vDec) +"^ADN,32,14^FDOF N° : ^FS^FO"+str(shiftLeft+160)+","+str(1*interLine-vDec) +"^ADN,32,14^FN1^FS"
        str2print+="^FO"+str(shiftLeft)+","+str(2*interLine-vDec) +"^ADN,32,14^FDPRODUIT : ^FS^FO"+str(shiftLeft+215)+","+str(2*interLine-vDec) +"^ADN,32,14^FN2^FS"
        str2print+="^FO"+str(shiftLeft)+","+str(3*interLine-vDec) +"^ADN,24,12^FN3^FS"
        str2print+="^FO"+str(shiftLeft)+","+str(4*interLine-vDec) +"^ADN,32,14^FDSANGLE : ^FS^FO"+str(shiftLeft+200)+","+str(4*interLine-vDec) +"^ADN,32,14^FN4^FS"
        str2print+="^FO"+str(shiftLeft)+","+str(5*interLine-vDec) +"^ADN,24,12^FN5^FS"
        str2print+="^FO"+str(shiftLeft)+","+str(6*interLine-vDec) +"^ADN,28,13^FDNombre de coupe : ^FS^FO"+str(shiftLeft+250)+","+str(6*interLine-vDec) +"^ADN,28,13^FN6^FS"
        str2print+="^FO"+str(shiftLeft)+","+str(7*interLine-vDec) +"^ADN,28,13^FDLongueur coupée : ^FS^FO"+str(shiftLeft+250)+","+str(7*interLine-vDec) +"^ADN,28,13^FN7^FS"
        str2print+="^FO"+str(shiftLeft)+","+str(8*interLine-vDec) +"^ADN,24,12^FDEmplacement : ^FS^FO"+str(shiftLeft+160)+","+str(8*interLine-vDec) +"^ADN,24,12^FN8^FS"
        str2print+="^XZ" # fin du format d'étiquette
        win32print.WritePrinter(self.printerDevice, str2print.encode("utf8")) #écrit le format d'étiquette 

    def printLabel(self, orderNum, productSku, productName, webSku, webName, partNum, partLength, emplacement):
        str2print="^XA\n" #debut étiquette
        str2print+="^XFFORMAT" #rappel du format enregistré
        str2print+="^FN1^FD"+orderNum+"^FS"
        str2print+="^FN2^FD"+productSku+"^FS"
        str2print+="^FN3^FD"+productName+"^FS"
        str2print+="^FN4^FD"+webSku+"^FS"
        str2print+="^FN5^FD"+webName+"^FS"
        str2print+="^FN6^FD"+str(partNum)+"^FS"
        str2print+="^FN7^FD"+partLength+"^FS"
        str2print+="^FN8^FD"+emplacement+"^FS"
        str2print+="^XZ" # fin du format d'étiquette 
        win32print.WritePrinter(self.printerDevice, str2print.encode("utf8")) #écrit l'étiquette 

    def endLabel(self):
        self.printerDevice.close() # ferme le canal d'impression et déclenche l'impression de ce qui précède
        del self.job

def newPrintLabel():
    zpl = ZPLLabel('Zebra ZP 450 CTP')
    zpl.printLabel("20009999", "1035691", "Harnais Energy TWIN ss porte outil L/XL",
                       "90008318", "SA/SANGLE NOIRE 20 MM", 35, "0.38m", "Bavaroise réglable")
    zpl.endLabel()

if __name__ == '__main__':
    app = newPrintLabel()

我想,冻结时缺少某些程序包或dll以使其运行。 我试图添加win32api,win32com,但它不会改变结果。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

尝试使用python win32print not printing的答案中的win32uiwin32con

在这种情况下,您还应该保持base的定义与原始问题相同(关于我对问题的评论)。

答案 1 :(得分:0)

针对尝试此类问题的人员。 我的代码编写不正确: 每个StartDocPrinter指令必须与EndDocPrinter指令相对应,显然这不会引起脚本问题,但会影响冻结版本。 因此,正确的指令线程必须类似于:

reduce