-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第一步去哪里设置,求回答 #19
Comments
看你贴的代码,你已经修改配置了呀?然后命令行运行这段python代码,挂在后端就好了。 |
第一个问题:运行脚本以后(修改部分为8888改为7860),
translatetranslating ###: G:/OneDrive/Zotero/Storage/IEE IEEE_VTS_Challenge/2023/Pre_2018_Model Based Energy Management and State Estimation for the Robotic Electric Vehicle ROboMObil.pdfTraceback (most recent call last): translatetranslating ###: G:/OneDrive/Zotero/Storage/IEE IEEE_VTS_Challenge/2023/Pre_2018_Model Based Energy Management and State Estimation for the Robotic Electric Vehicle ROboMObil.pdfTraceback (most recent call last): |
这个不需要在浏览器打开,挂在后台就好了。 |
我将pdf2zh安装在通过conda创建的虚拟环境里面,如果通过激活conda环境后,使用pdf2zh -i 可以在浏览器正常使用,也能正常翻译。 |
你可以试试打开命令行工具(cmd),先在命令行激活虚拟环境,然后在命令行工具里执行这个脚本试试。 或者尝试另一个方法: |
我想请问第一步的脚本配置怎么实现
具体操作步骤是什么
from flask import Flask, request, jsonify
import os
import base64
import subprocess
from flask import Flask, send_file, abort
from pypdf import PdfWriter, PdfReader
from pypdf.generic import RectangleObject
import sys
####################################### 配置 #######################################
pdf2zh = "pdf2zh" # 设置pdf2zh指令: 默认为'pdf2zh'
thread_num = 4 # 设置线程数: 默认为4
port_num = 7860 # 设置端口号: 默认为8888
service = 'bing' # 设置翻译服务: 默认为bing
translated_dir = "./translated/" # 设置翻译文件的输出路径(临时路径, 可以在翻译后删除)
config_path = './config.json' # 设置配置文件路径
######################################################################################
def get_absolute_path(path): # 获取绝对路径
if os.path.isabs(path):
return path
else:
return os.path.abspath(path)
def get_file_from_request(request): # 从request中解析pdf文件
data = request.get_json()
path = data.get('filePath')
path = path.replace('\', '/') # 把所有反斜杠\替换为正斜杠/ (Windows->Linux/MacOS)
if not os.path.exists(path):
file_content = data.get('fileContent')
input_path = os.path.join(translated_dir, os.path.basename(path))
if file_content:
if file_content.startswith('data:application/pdf;base64,'): # 移除 Base64 编码中的前缀(如果有)
file_content = file_content[len('data:application/pdf;base64,'):]
file_data = base64.b64decode(file_content) # 解码 Base64 内容
with open(input_path, 'wb') as f:
f.write(file_data)
else:
input_path = path
return input_path
app = Flask(name)
@app.route('/translate', methods=['POST'])
def translate():
print("### translate ###")
input_path = get_file_from_request(request)
try:
os.makedirs(translated_dir, exist_ok=True)
print("### translating ###: ", input_path)
@app.route('/translatedFile/')
def download(filename):
directory = translated_dir
abs_directory = get_absolute_path(directory)
file_path = os.path.join(abs_directory, filename)
if not os.path.isfile(file_path):
return "File not found", 404
return send_file(file_path, as_attachment=True, download_name=filename)
新增了一个cut pdf函数,用于切割双栏pdf文件
def split_and_merge_pdf(input_pdf, output_pdf):
writer = PdfWriter()
if 'dual' in input_pdf:
reader1_1 = PdfReader(input_pdf)
reader1_2 = PdfReader(input_pdf)
reader2_1 = PdfReader(input_pdf)
reader2_2 = PdfReader(input_pdf)
for i in range(0, len(reader1_1.pages), 2):
page1_1 = reader1_1.pages[i]
page1_2 = reader1_2.pages[i]
page2_1 = reader2_1.pages[i+1]
page2_2 = reader2_2.pages[i+1]
新增了一个cut接口,用于切割双栏pdf文件
@app.route('/cut', methods=['POST'])
def cut():
print("### cut ###")
input_path = get_file_from_request(request)
try:
os.makedirs(translated_dir, exist_ok=True)
print("### cutting ###: ", input_path)
abs_translated_dir = get_absolute_path(translated_dir)
translated_path = os.path.join(abs_translated_dir, os.path.basename(input_path).replace('.pdf', '-cut.pdf'))
split_and_merge_pdf(input_path, translated_path)
if not os.path.exists(translated_path):
raise Exception("failed to generate cutted files")
return jsonify({'status': 'success'}), 200
except Exception as e:
print(f"Error: {e}")
return jsonify({'status': 'error', 'message': str(e)}), 500
if name == 'main':
if len(sys.argv) > 1: # 命令行参数1: service
service = sys.argv[1]
if len(sys.argv) > 2: # 命令行参数2: thread_num
thread_num = int(sys.argv[2])
app.run(host='0.0.0.0', port=port_num)
The text was updated successfully, but these errors were encountered: