BeamBullet commited on
Commit
6f8b89f
·
verified ·
1 Parent(s): 7974030
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, send_file
2
+ from pdf2docx import Converter
3
+ import os
4
+
5
+ app = Flask(__name__)
6
+ UPLOAD_FOLDER = './uploads'
7
+ CONVERTED_FOLDER = './converted'
8
+ ALLOWED_EXTENSIONS = {'pdf'}
9
+
10
+ if not os.path.exists(UPLOAD_FOLDER):
11
+ os.makedirs(UPLOAD_FOLDER)
12
+
13
+ if not os.path.exists(CONVERTED_FOLDER):
14
+ os.makedirs(CONVERTED_FOLDER)
15
+
16
+ def allowed_file(filename):
17
+ return '.' in filename and \
18
+ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
19
+
20
+ @app.route('/')
21
+ def upload_form():
22
+ return '''
23
+ <!doctype html>
24
+ <title>Upload a PDF to Convert</title>
25
+ <h1>Upload a PDF to Convert to Word</h1>
26
+ <form method=post enctype=multipart/form-data>
27
+ <input type=file name=file>
28
+ <input type=submit value=Upload>
29
+ </form>
30
+ '''
31
+
32
+ @app.route('/', methods=['POST'])
33
+ def upload_file():
34
+ if 'file' not in request.files:
35
+ return 'No file part'
36
+ file = request.files['file']
37
+ if file.filename == '':
38
+ return 'No selected file'
39
+ if file and allowed_file(file.filename):
40
+ filepath = os.path.join(UPLOAD_FOLDER, file.filename)
41
+ file.save(filepath)
42
+ docx_filename = os.path.splitext(file.filename)[0] + '.docx'
43
+ docx_filepath = os.path.join(CONVERTED_FOLDER, docx_filename)
44
+ converter = Converter(filepath)
45
+ converter.convert(docx_filepath)
46
+ converter.close()
47
+ os.remove(filepath) # Remove the uploaded PDF
48
+ return send_file(docx_filepath, as_attachment=True)
49
+ return 'Invalid file type'
50
+
51
+ if __name__ == '__main__':
52
+ app.run(debug=True)