|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
app.py:
import os
import pathlib
import patoolib
from PyPDF2 import PdfFileReader, PdfFileWriter
from flask import Flask
from flask import render_template,request,send_file
app = Flask(__name__)
BASE_DIR = pathlib.Path()
@app.route('/',methods=["GET", "POST"])
def home():
if request.method == "POST":
file = request.files["pdf"]
pdf = PdfFileReader(file)
splitted = []
for i in range(pdf.getNumPages()):
w = PdfFileWriter()
w.addPage(pdf.getPage(i))
with open(BASE_DIR / "tmp" / f"{i+1}.pdf", 'wb') as out:
w.write(out)
splitted.append(f"tmp{i+1}.pdf")
patoolib.create_archive (f"tmp/pdf.zip", splitted)
return send_file(BASE_DIR /"tmp"/"pdf.zip", as_attachment=True)
return render_template("home.html")
app.run()
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF 分割</title>
</head>
<body>
<form action="." method="post" enctype="multipart/form-data" >
<input type="file" name="pdf" id="pdf">
<input type="submit" value="上传">
</form>
</body>
</html>
|
|