利用Python本地搭建自助打印网站
环境搭建
本网站环境需要Python3以上,以及Python插件flask、WSGI,WPS Office2019以上
代码部分
from flask import Flask
from flask import request
from wsgiref.simple_server import make_server
import os
import win32print
import win32api
def printer_loading(filename,chooseDuplex,chooseColor,chooseCopies):
# name = win32print.GetDefaultPrinter()
name = "Canon TS8000 series"
# printdefaults = {"DesiredAccess": win32print.PRINTER_ACCESS_ADMINISTER}
printdefaults = {"DesiredAccess": win32print.PRINTER_ACCESS_USE}
handle = win32print.OpenPrinter(name, printdefaults)
level = 2
attributes = win32print.GetPrinter(handle, level)
print("old Duplex = %d" % attributes['pDevMode'].Duplex)
print("old Orientation = %d" % attributes['pDevMode'].Orientation)
print("old color = %d" % attributes['pDevMode'].Color)
print("old copies = %d" % attributes['pDevMode'].Copies)
attributes['pDevMode'].Duplex = int(chooseDuplex) # 1单面 2长边装订双面 3短边装订双面
attributes['pDevMode'].Orientation = 2 #纵向打印(暂时无法横向打印)
attributes['pDevMode'].Color = int(chooseColor) # 0彩色 1黑白
attributes['pDevMode'].Copies = int(chooseCopies) #份数
try:
win32print.SetPrinter(handle, level, attributes, 0)
except:
print("new Duplex = %d" % attributes['pDevMode'].Duplex)
print("new Orientation = %d" % attributes['pDevMode'].Orientation)
print("new color = %d" % attributes['pDevMode'].Color)
print("new copies = %d" % attributes['pDevMode'].Copies)
res = win32api.ShellExecute(0, 'print', filename, None, '.', 0)
win32print.ClosePrinter(handle)
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>电竞小生自助打印网站</title>
</head>
<body>
<p align="center">
<form class="mb-3" action="/printExcute" method="post" enctype="multipart/form-data">
<div>
<br/>
<input class="form-control form-control-lg" id="formFileLg" type="file" name="printfile">
<br/>
<select class="form-select" aria-label="Default select example" name="chooseDuplex">
<option selected value='1'>请选择单双面类型</option>
<option value='1'>单面打印</option>
<option value='2'>双面打印(长边装订)</option>
<option value='3'>双面打印(短边装订)</option>
</select>
<br/>
<select class="form-select" aria-label="Default select example" name="chooseColor">
<option selected value='0'>请选择是否为黑白或彩色打印</option>
<option value='0'>彩色</option>
<option value='1'>黑白</option>
</select>
<br/>
<select class="form-select" aria-label="Default select example" name="chooseCopies">
<option value='1'>打印份数:默认1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
</select>
<br/>
<input class="btn btn-primary mb-3" type="submit" name="mufile" value="提交打印文件">
</div>
</form>
</p>
</body>
</html>
'''
@app.route('/printExcute', methods=['POST'])
def printExcute():
#接收文件,准备保存打印
f = request.files.get('printfile')
print(f.filename)
path = r'./print_temp_file'
file_path = os.path.join(path, f.filename)
f.save(file_path) # 保存到指定目录
print("成功保存")
# 这里可以添加其他文件格式类型
if file_path.endswith("docx") or file_path.endswith("doc") or file_path.endswith("pdf"):
printer_loading(file_path,request.form['chooseDuplex'],request.form['chooseColor'],request.form['chooseCopies'])
print('打印机启动完毕')
return '<p>打印提交成功</p>'
if __name__ == '__main__':
# app.run(debug=True,host='127.0.0.1',port=8000)
server = make_server('127.0.0.1', 5000, app)
server.serve_forever()
# print('http://127.0.0.1:5000')
app.run(debug=True)
创建接收打印文件的文件夹
在同级目录下创建print_temp_file文件夹。
使用
浏览器输入http://127.0.0.1:5000,进入页面
打印完成
总结
该打印程序操作简单,后续可以通过内网穿透达到外网打印。但是只能满足基础功能,各位开发者可以结合自己需要对此改进