根据excel批量生成打印卡片功能
这天某大人在企业微信跟我说上级领导要她打印整个工厂的临时出入车控制卡片,发给我一个资料,让我看看能不能帮她搞一下,资料就如下:
我一看,我操,原来是整个工厂好几百人的临时车辆管理证的打印!原来的他们是怎么打印的?都是ctrl+c和ctrl+v搞定,这得花多少时间的呀?!不过真的是没办法,国企的嘛,也在努力转型数字化,对于学了python的我,对于这样的一种工厂简直so easy!!其实我此前都是参考网上的做法去将excel的内容同步到word里面进行批量打印的,但是这一次由于证件的大小规格等,在word里面真的不方便编辑!怎么样可以某大人可以轻松地即刻使用到我的创作成果呢?其实有一个比较用的方法就是使用chrome的打印功能,我在远程直接写好应用,远端调用本地的打印功能,也不需要某大人怎么样部署,怎么样调试等,何乐而不为的呢?
那么我们正式开始的吧!
首先需要用到的框架有目前最流行的flask框架,为了配合前端的bootstrap框架的jquery,本地打印需要另外导入一个jquery的打印框架—printThis.js,项目整体架构如下:
其实在pycharm当中只需要新建一个flask项目即可:
还有的是需要用到一个外部库–xlrd将excel的内容上传至服务器,将内容解释为list返回到前端进行显示,在页面只需要三个按钮即可完成其相关功能,页面如下:
通过点击上传文件,即可显示其所有的excel内容:
由于只是想快速实现相关功能,页面并没有做过多的美化
通过点击全选,或者勾选其中一部分,即可对需要打印的条目进行打印。
不多说了,直接上代码
app.py
import xlrd
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/', methods=["GET", "POST"])
def hello_world():
ls = []
if request.method == "GET":
return render_template("index.html", ls=ls)
else:
file = request.files.get("excel_file")
try:
book = xlrd.open_workbook(file_contents=file.read())
sheet = book.sheet_by_index(0)
for i in range(1, sheet.nrows):
c = []
for j in range(sheet.ncols):
c.append(sheet.cell(i, j).value)
ls.append(c)
return render_template("index.html", ls=ls)
except Exception as e:
return str(e)
if __name__ == '__main__':
app.run(host='0.0.0.0',port=80)
html代码,
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="static/bootstrap/css/bootstrap.css">
</head>
<body>
<div class="container">
<div class="col-md-12">
<form enctype="multipart/form-data" method="post">
<input type="file" name="excel_file" accept=".xls,.xlsx" id="excel_file">
<button class="btn btn-primary" type="submit">提交</button>
</form>
<button class="btn btn-warning" id="all-print">打印全部</button>
</div>
<div class="container mt-3">
<table class="table table-bordered table-responsive table-hover table-condensed">
<tr>
<td>选择
<input type="checkbox" id="checkall">
</td>
<td>序号</td>
<td>部门</td>
<td>科室</td>
<td>系</td>
<td>姓名</td>
<td>岗位名称</td>
<td>考试通过时间</td>
<td>准驾证号</td>
</tr>
{% for l in ls %}
<tr>
<td>{{ ls.index(l) }}<input type="checkbox" name="test"></td>
{% for c in l %}
<td>{{ c }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
<div class="container" id="pull">
<div style="width:1487px;" class="border row" id="print_model">
</div>
</div>
</div>
<script src="static/bootstrap/jquery.min.js"></script>
<script src="static/bootstrap/js/bootstrap.bundle.js"></script>
<script src="static/printThis.js"></script>
<script>
$(function () {
$("#checkall").click(function () {
if (this.checked) {
$("input[name='test']:checkbox").each(function () {
$(this).prop("checked", true);
})
} else { //反之 取消全选
$("input[name='test']:checkbox").each(function () {
$(this).prop("checked", false);
})
}
});
});
function create_tab(parment, username, certifica_number) {
$('<div class="col-6 border">\n' +
' <div class="row" style="height:714px">\n' +
' <div class="col-6 border" style="height:100%;background-color:#ffff9b;">\n' +
' <div style="height: 15%">\n' +
' <img src="static/logo.png" alt="" style="width: 100%;">\n' +
' <img src="static/certifica.png" alt=""\n' +
' style="position: relative;left: 238px;top:79px;height: 250px">\n' +
' </div>\n' +
' <div style="height: 40%;margin: 0px 80px;"\n' +
' class="row text-center align-items-center border">\n' +
' <h1 style="color: blue">移车支援业务临时证件</h1>\n' +
' </div>\n' +
' <div class="row mt-4 border" style="color: blue">\n' +
' <div class="text-center h2">\n' +
' 普通移动准驾资格证\n' +
' </div>\n' +
'\n' +
' </div>\n' +
' <div class="border row h2" style="margin-bottom:0px;line-height:1.5">\n' +
' 部门:'+parment+' \n' +
' </div>\n' +
' <div class="border row h2" style="margin-bottom:0px;line-height:1.5">姓名:'+username+'</div>\n' +
' <div class="border row h2" style="margin-bottom:0px;line-height:1.5">证件编号:'+certifica_number+'</div>\n' +
' <div class="border row h2" style="margin-bottom:0px;line-height:1.5">有效期:2022.6.2</div>\n' +
' <div class="border row h2" style="margin-bottom:0px;line-height:1.5">发证日期:2021.12.2</div>\n' +
' </div>\n' +
' <div class="col-6 border" style="font-size: 30px;">\n' +
' <h2 class="text-center mt-4 mb-4">使 用 规 定</h5>\n' +
'\n' +
' <p>1、此证只限于生管安排的移车支援业务使用,只限于支援业务区域内移动驾驶,不允许检测线设备与试车跑道驾驶产品车。</p>\n' +
'\n' +
' <p>2、此资格证必须与国家驾驶证一并使用。</p>\n' +
'\n' +
' <p>3、在车间内移动产品车时必须佩带本资格证。</p>\n' +
'\n' +
' <p>4、严格遵守公司内相关产品车行驶的各项安全规定。</p>\n' +
' </div>\n' +
' </div>\n' +
' </div>').appendTo($("#print_model"))
};
$(function () {
$("#all-print").click(function () {
$('#print_model').empty();
$('input[name="test"]:checked').each(function () {
var self=$(this).parent().parent();
var department=self.children().eq(2).html();
var username=self.children().eq(5).html();
var number=self.children().eq(8).html();
console.log(department,username,number);
create_tab(department,username,number);
});
$("#pull").printThis({
debug: false,
importCSS:true,
importStyle: true,
printContainer: true,
loadCSS: "static/bootstrap/css/bootstrap.css",
pageTitle: "打印标签",
printDelay: 333,
formValues: false
});
})
})
</script>
</body>
</html>
整个项目我只是花了一天的时间左右就做出来了(可能是水平有限的原因的吧,大佬有可能2个小时就做出来了),其中最难的部分是debug页面的css,因为调这个css真的非常花时间!!!是非常花时间!!!上面的整个项目已经上传到gitee,gitee连接如下,有需要的小伙伴们就拿去参考一下吧
https://gitee.com/guanjiawen_008/print_certificate