实习记录(7)——一些小工具

最近猛加班,写了一些通用小工具

一、zebra打印机使用zpl语言打印

首先,调包侠调了两个包,zebra和zpl这两个包,需要pip一下的

import zpl
from zebra import Zebra

def generate_label(str1,str2):
    l = zpl.Label(10,40,8) #标签的高度,宽度,dpmm
    height = 0
    l.origin(7,1)
    l.write_text(str1, char_height=2, char_width=1,font='1')
    l.endorigin()

    height += 3
    l.origin(7, height)
    l.barcode('C',str2,35,check_digit='Y',print_interpretation_line='N')
    l.endorigin()

    # height +=5
    # l.origin(7, height)
    # l.write_text(str2, char_height=2, char_width=1,font='1')
    # l.preview()
    return l.dumpZPL()



def print_label(label):
    z=Zebra('ZDesigner ZD888-203dpi ZPL')#打印机名字
    z.output(label)



if __name__=='__main__':
    str2='hanjingxuan is pig'
    zpl_line=generate_label("yes",str2)
    # if len(str2)>11:
    #     potion=zpl_line.index('^BC')
    #     line1=zpl_line[0:potion]
    #     line2=zpl_line[potion:]
    #     new_line=line1+'^BY 1'+line2
    #     potion=zpl_line.index('^BC')
    #     line1=zpl_line[0:potion]
    #     line2=zpl_line[potion:]
    #     new_line=line1+'^BY 2'+line2
    # potion1=zpl_line.index('^A')
    # line1=zpl_line[0:potion1+2]
    # line2=zpl_line[potion1+3:]
    # zpl_line=line1+'1'+line2
    potion2=zpl_line.index('^BC')
    line_left=zpl_line[0:potion2]
    line_right=zpl_line[potion2:]
    new_line=line_left+'^BY 1'+line_right
    print(new_line)
    print_label(new_line)

想要预览一下你的标签样式,注释的‘l.preview()’取消一下。

问:如何测试zpl语言在你想要的标签上是啥样的?

An Introduction to ZPL (labelary.com)

上面这个页面简述了一下一些zpl基本的语法,里面对应有example,可以输入zpl绘制出你想要的条码或者图形,功能还是很强大的,碰到一些问题问打印机的技术人员,他问我是不是开发的,我说是的,他直接给我一个1000多页的zpl中文手册,一点捷径都不给我留。

zebra · PyPI

这是zebra的官方文档(?,一些方法的简单介绍,反正我看不太懂。

zpl的源码建议在GitHub上下载下来看一下,有一些参数设置,看了就会设置了,不然操作字符串还是挺烦的。

二、python发邮件

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
import config_tool

class SMTP_conn():
    def __init__(self):
        config=config_tool.read_config('config.cfg')
        
        self.host=config['host']
        self.sendAddr=config['sendAddr']
        self.reciveAddr=[]
        self.password=config['password']
        self.port=config['port']
        self.msg=MIMEMultipart()

    def set_head(self,subject):
        self.msg["Subject"] = subject
        self.msg["From"] = self.sendAddr #发送人
        self.msg["To"] = self.reciveAddr[0] #接收人

    def get_msg(self):
        return self.msg

    def append_reciveAddr(self,reciveAddr):
        self.reciveAddr.append(reciveAddr)

    def create_main_content(self,content):
        main_content = MIMEText(content,'plain','utf-8')
        self.msg.attach(main_content)
    
    def create_attachFile(self,filename):
        attachFile=MIMEText(open(filename,'rb').read(),'base64','utf-8')
        attachFile["Content-Type"]='application/octet-stream'
        attachFile["Content-Disposition"] = 'attachment; filename={}'.format(filename)
        self.msg.attach(attachFile)

    def send_Email(self,msg):
        s = smtplib.SMTP_SSL(self.host,int(self.port)) 
        s.login(self.sendAddr, self.password)
        s.sendmail(self.sendAddr, self.reciveAddr, msg.as_string())
        s.quit()
    
def send(reciveAddr,head,main_content,attachFile):
    conn=SMTP_conn()
    conn.append_reciveAddr(reciveAddr)
    conn.set_head(head)
    conn.create_main_content(main_content)
    conn.create_attachFile(attachFile)
    conn.send_Email(conn.get_msg())

附件那边可能有点问题,别看,另外有一个读配置文件的小工具,直接就是一个复制黏贴。

#config_tool.py
from configparser import ConfigParser as cp

def read_config(filename):
    config_parser = cp() # 初始化对象
    config_parser.read(filename) # 读取配置文件
    config = config_parser["default"]
    return config

配置文件格式可以参照,直接填参数就行,不用引号

#config.cfg

[default]
host=
port=
sendAddr=
password=

三、RabbitMQ连接工具

import pika

class MQ():
    def __init__(self):
        self.hostname='*'    #主机名
        self.port = 5672
        self.Connection = pika.BlockingConnection(
            pika.ConnectionParameters(
                host=self.hostname,
                port=self.port,
                virtual_host='/',  # 虚拟主机
                credentials=pika.PlainCredentials('guest', 'guest')
            )
        )
    
    def create_send_channel(self,exchange,queue,routing_key):
        channel=self.Connection.channel()
        channel.exchange_declare(exchange=exchange,exchange_type='direct', durable=True)
        channel.queue_declare(queue=queue, durable=True)
        channel.queue_bind(queue=queue, exchange=exchange, routing_key=routing_key)
        print('channel created successfully')
        return channel
        
    def create_recive_channel(self,queue):
        channel=self.Connection.channel()
        channel.queue_declare(queue=queue, durable=True)
        return channel
        
    def close_conn(self):
        self.Connection.close()
        print('MQ_connect closed')
        

项目需要又衍生出一些操作xml文件的小工具

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element


#将xml中的信息发送到MQ
def send_xml(channel,exchange,routing_key,message_name):
    with open("{}.xml".format(message_name),'rt',encoding='utf-8') as fp:
        lines=fp.readlines()
        channel.basic_publish(exchange=exchange,routing_key=routing_key,body=''.join(lines))

def body2xml(body):    #rabbitmq中的信息转换成xml格式
    body_str=str(body,'utf-8')
    print(body_str)
    doc = ET.fromstring(body_str)
    return doc

#读tag的值
def read_tag_value(doc,tag):
    for child in doc:
        if child.tag==tag:
            return child.text
    
# def read_tag_attr(doc,attrib_name):
#     return doc.attrib[attrib_name]

建xml文件的方法有很多文章,可以自行去查看,有一点要注意的就是如果是ET.parse()方法导的xml,需要多进行一个getroot()方法。

四、其他

三个礼拜我已经加班两个周六了,已经能熟练调包了,今天需要给新服务器开个数据库远程连接,这个可以单独写一个,不过确实以前用云服务器方便多了,控制台啥都能开。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值