dschandra commited on
Commit
23c1fb0
·
verified ·
1 Parent(s): 047f763

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, render_template, send_file
2
+ import os
3
+ from docx2pdf import convert as docx2pdf_convert
4
+ from xlsx2pdf import xlsx2pdf
5
+
6
+ app = Flask(__name__)
7
+ UPLOAD_FOLDER = 'uploads'
8
+ CONVERTED_FOLDER = 'converted'
9
+
10
+ # Ensure upload and converted directories exist
11
+ if not os.path.exists(UPLOAD_FOLDER):
12
+ os.makedirs(UPLOAD_FOLDER)
13
+
14
+ if not os.path.exists(CONVERTED_FOLDER):
15
+ os.makedirs(CONVERTED_FOLDER)
16
+
17
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
18
+ app.config['CONVERTED_FOLDER'] = CONVERTED_FOLDER
19
+
20
+ @app.route('/', methods=['GET', 'POST'])
21
+ def index():
22
+ if request.method == 'POST':
23
+ file = request.files['file']
24
+ if file:
25
+ file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
26
+ file.save(file_path)
27
+
28
+ # Convert the file to PDF based on its extension
29
+ file_extension = file.filename.split('.')[-1].lower()
30
+ converted_file_path = os.path.join(app.config['CONVERTED_FOLDER'], f"{os.path.splitext(file.filename)[0]}.pdf")
31
+
32
+ try:
33
+ if file_extension == 'doc' or file_extension == 'docx':
34
+ docx2pdf_convert(file_path, converted_file_path)
35
+ elif file_extension == 'xlsx':
36
+ xlsx2pdf(file_path, converted_file_path)
37
+ else:
38
+ return "Unsupported file type!"
39
+
40
+ return send_file(converted_file_path, as_attachment=True)
41
+ except Exception as e:
42
+ return str(e)
43
+
44
+ return render_template('index.html')
45
+
46
+ if __name__ == '__main__':
47
+ app.run(host='0.0.0.0', port=7860)