File size: 963 Bytes
dbf9148
40c3856
e4d75fe
dbf9148
e4d75fe
40c3856
 
 
dbf9148
 
 
 
e4d75fe
dbf9148
 
 
 
40c3856
dbf9148
 
 
e4d75fe
dbf9148
 
e4d75fe
dbf9148
 
 
 
e4d75fe
dbf9148
40c3856
dbf9148
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
import os
from pdf2docx import Converter

app = Flask(__name__)
CORS(app)

UPLOAD_FOLDER = "uploads"
CONVERTED_FOLDER = "converted"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(CONVERTED_FOLDER, exist_ok=True)

@app.route("/convert", methods=["POST"])
def convert_pdf_to_docx():
    if "file" not in request.files:
        return jsonify({"error": "No file uploaded"}), 400

    pdf_file = request.files["file"]
    pdf_path = os.path.join(UPLOAD_FOLDER, pdf_file.filename)
    pdf_file.save(pdf_path)

    docx_filename = os.path.splitext(pdf_file.filename)[0] + ".docx"
    docx_path = os.path.join(CONVERTED_FOLDER, docx_filename)

    # Convert using pdf2docx
    cv = Converter(pdf_path)
    cv.convert(docx_path, start=0, end=None)
    cv.close()

    return send_file(docx_path, as_attachment=True)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)