提交 947e2ade 作者: martin

ocrmypdf程序增加

上级 f50e48e9
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传文件</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
</body>
</html>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/6/17 09:52
# @Author : Martin
"""
功能描述:
开放服务端口9999,实现接收一个文件,内部通过shell命令处理后,将文件返回
目的:主要完成ocrmypddf服务的组件化改造
现状:当前ocrmypdf没有监听端口,无法实现接口调用实现pdf文件的转换。
目标:通过调用该服务接口,实现对传入文件的处理,并返回
"""
from flask import Flask, request, send_file
import os
from werkzeug.utils import secure_filename
from subprocess import check_output as co
# 定义全局变量
# 脚本所在文件夹
local_dir = os.path.dirname(__file__)
# 接收上传的文件存储位置
upload_dir = os.path.join('/opt', 'upload')
# upload_dir = os.path.join(local_dir, 'upload')
# 如果上传的文件位置不存在则创建
os.makedirs(upload_dir, exist_ok=True)
# 创建Flask对象
app = Flask(__name__)
print(f"upload file dir is {upload_dir}")
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return '没有文件部分'
file = request.files['file']
if file.filename == '':
return '没有选择文件'
if file.filename.endswith('.pdf'):
# 在这里处理你的文件,例如保存到服务器
filename = secure_filename(file.filename) # 确保文件名安全
src_file_path = os.path.join(upload_dir, filename)
file.save(src_file_path) # 保存文件
dst_file_path = process_file(src_file_path)
if dst_file_path:
return send_file(dst_file_path, as_attachment=True)
else:
return 'convert fail'
else:
return '上传文件不是pdf文件,当前只支持pdf文件处理'
@app.route('/')
def hello():
return 'hello world...'
def process_file(src_file_path):
dst_file_path = src_file_path[0:-4]+'_mod.pdf'
flag = os.system(f'/usr/bin/ocrmypdf --force-ocr -l chi_sim+eng {src_file_path} {dst_file_path}')
if flag == 0:
return dst_file_path
else:
return None
if __name__ == '__main__':
app.run(host='0.0.0.0')
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/6/17 10:35
# @Author : Martin
"""
功能描述:
"""
from subprocess import check_output as co
import os
# abc = co('cmd /k dir').decode('utf-8')
# print(abc)
# if os.system('echo "abc" >1.txt') == 0:
# print('success')
# else:
# print('fail')
# os.system('dir')
# print(res)
# local_dir = os.path.dirname(__file__)
# print(local_dir)
a = 'abc.pdf'
print(a[0:-4]+'_mod.pdf')
if None:
print('true')
else:
print('false')
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论