dschandra commited on
Commit
41a65e8
·
verified ·
1 Parent(s): 8dd1445

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -2
app.py CHANGED
@@ -1,7 +1,9 @@
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'
@@ -17,6 +19,30 @@ if not os.path.exists(CONVERTED_FOLDER):
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':
@@ -33,7 +59,7 @@ def index():
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
 
 
1
  from flask import Flask, request, render_template, send_file
2
  import os
3
  from docx2pdf import convert as docx2pdf_convert
4
+ from openpyxl import load_workbook
5
+ from reportlab.lib.pagesizes import letter
6
+ from reportlab.pdfgen import canvas
7
 
8
  app = Flask(__name__)
9
  UPLOAD_FOLDER = 'uploads'
 
19
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
20
  app.config['CONVERTED_FOLDER'] = CONVERTED_FOLDER
21
 
22
+ def convert_xlsx_to_pdf(input_file, output_file):
23
+ workbook = load_workbook(input_file)
24
+ sheet = workbook.active
25
+
26
+ c = canvas.Canvas(output_file, pagesize=letter)
27
+ width, height = letter
28
+
29
+ x_offset = 50
30
+ y_offset = height - 50
31
+ line_height = 20
32
+
33
+ for row in sheet.iter_rows(values_only=True):
34
+ x = x_offset
35
+ y = y_offset
36
+ for cell in row:
37
+ c.drawString(x, y, str(cell))
38
+ x += 100 # Adjust column width
39
+ y_offset -= line_height
40
+ if y_offset < 50:
41
+ c.showPage()
42
+ y_offset = height - 50
43
+
44
+ c.save()
45
+
46
  @app.route('/', methods=['GET', 'POST'])
47
  def index():
48
  if request.method == 'POST':
 
59
  if file_extension == 'doc' or file_extension == 'docx':
60
  docx2pdf_convert(file_path, converted_file_path)
61
  elif file_extension == 'xlsx':
62
+ convert_xlsx_to_pdf(file_path, converted_file_path)
63
  else:
64
  return "Unsupported file type!"
65