Python 調用打印機API - APISample Python 标簽打印範例代碼,,Python 打印, Python 實現打印,Python調用打印機, Python 打印API,Python 打印報表,Python 标簽打印,Python 單據打印 ,CCPrintingAPI , Python 打印機調用 範例代碼,Python 标簽打印,條碼,二維碼标簽,Python 單據打印,很(hěn)容易集成。
CCPrintingAPI XI
輕量、簡潔、穩定、可(kě)靠,通(tōng)用的打印機API接口,支持近百個(gè)品牌,數(shù)千個(gè)型号打印機。
打印機接口API Python 範例代碼
Python
CCPrintingAPI Python API打印範例代碼,Python調用打印機,Python 調用CCPrintingAPI打印機接口API。

1. 引用 CCPrintingApiNet.dll
引用CCPrintingApiNet.dll後,即可(kě)使用API的相關方法,進行(xíng)設置和(hé)打印操作(zuò)。
clr.AddReference("CCPrintingApiNet")
from Chicore import CCPrintingApiNet
from Chicore import ConfigPrintSettings

#獲取所有(yǒu)打印機
getprinters(m_OpenId,m_Tokens,Action[Object](printers_callback))
#打印
printex(m_OpenId,m_Tokens,Action[Object](print_callback))
#獲取HID
gethid(m_OpenId,m_Tokens,Action[Object](hid_callback)) 
2. 獲取打印機
獲取計(jì)算(suàn)機上(shàng)所有(yǒu)打印機, 打印時(shí),指定該列表的打印機名稱。如果能确保打印機名稱不變,該操作(zuò)也可(kě)省去。
#獲取打印機回調
def printers_callback(arg):
    status = arg.Status
    arg0 = arg.Message
    sessionid = arg.SessionId
    print("Excute Status: {:d}, SessionId:{:s}".format(status, sessionid))
    if arg0 == None:
        return
    #獲取成功,顯示所有(yǒu)打印機
    if status == 1 :
        for i in range(1,len(arg0)):
            print(arg0[i])
#獲取打印機    
def getprinters(openid,token,callback):
    cfgData = ConfigPrintSettings()
    cfgData.XAction = "Printers"
    cfgData.XOpenId = openid
    cfgData.XTokens = token
    CCPrintingApi.ExcuteEx(cfgData,callback)
3. 打印
進行(xíng)相關參數(shù)設置後(指定打印機,設置模闆,設置打印數(shù)據),将指令通(tōng)過API傳給打印機。
#打印回調
def print_callback(arg):
    status = arg.Status
    arg0 = arg.Message
    sessionid = arg.SessionId
    print("Excute Status: {:d}, SessionId:{:s}".format(status, sessionid))
    if arg0 == None:
        return
   #打印成功
    if status == 1 :
        print("{:s}:{:s}".format(arg0[0],arg0[1]))

#執行(xíng)打印 
def printex(openid,token,callback):
    cfgData = ConfigPrintSettings()
    cfgData.XAction = "Print"
    cfgData.XOpenId = openid
    cfgData.XTokens = token
    cfgData.PrinterName = "Honeywell PC42t (203 dpi) - DP"

    #使用模闆
    cfgData.TemplateMode = 1
    cfgData.TemplateURL = "http://img.51321.cn/web/chicore/VPrinting/API"
    cfgData.TemplateName ="183601-70X40.Lblx"
    #傳入數(shù)據
    newprtdata = "987650002;CCPrintingAPI;測試産品 API新接口"
    prtdata = "[{sku:'OBJ100001',name:'測試1',kw:'D12-1',bh:'RKD123451'},{sku:'OBJ100002',name:'測試2',kw:'D12-2',bh:'RKD123452'}]"
    cfgData.PrintDataType = 1
    cfgData.PrintData = prtdata
    CCPrintingApi.ExcuteEx(cfgData,callback)
4. 讀取客戶端HID
讀取安裝CCPrintingAPI的客戶端計(jì)算(suàn)機HID,該HID可(kě)以用來(lái)标識計(jì)算(suàn)機。該方法,根據需要使用。
#獲取HID
def hid_callback(arg):
    status = arg.Status
    arg0 = arg.Message
    sessionid = arg.SessionId
    print("Excute Status: {:d}, SessionId:{:s}".format(status, sessionid))
    if arg0 == None:
        return
    if status == 1 :
        print("{:s}:{:s}".format(arg0[0],arg0[1]))
    
def gethid(openid,token,callback):
    cfgData = ConfigPrintSettings()
    cfgData.XAction = "HID"
    cfgData.XOpenId = openid
    cfgData.XTokens = token
    CCPrintingApi.ExcuteEx(cfgData,callback)