from flask import Flask, request, render_template, send_file from io import BytesIO, StringIO import ezdxf import openpyxl import os import sys app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def upload_file(): if request.method == 'POST': file = request.files['file'] if file.filename.endswith('.xls') or file.filename.endswith('.csv') or file.filename.endswith('.xlsx'): # Create a text file with the uploaded file name velikost = request.form['range'] print(velikost) body = nacti_data_z_excelu(file) #print(body) dxf_file = exportuj_do_dxf(body, velikost) dxf_file.seek(0) return send_file( path_or_file=dxf_file, as_attachment=True, download_name='hotovy.dxf', mimetype='application/octet-stream' ) else: return render_template('upload_form.html', no_file=True) return render_template('upload_form.html') def nacti_data_z_excelu(soubor, listname="List1"): wb = openpyxl.load_workbook(soubor, data_only=True) sheet = wb[listname] body = [] for row in sheet.iter_rows(min_row=2, values_only=True): if row[0] and row[1] is not None and row[2] is not None: label = str(row[0]) try: x = float(row[1]) y = float(row[2]) z = float(row[3]) if len(row) > 3 and row[3] is not None else 0 body.append((label, x, y, z)) except ValueError: continue return body def exportuj_do_dxf(body, velikost) -> BytesIO: doc = ezdxf.new() msp = doc.modelspace() doc.layers.new(name="BODY", dxfattribs={"color": 7}) doc.layers.new(name="POPISKY", dxfattribs={"color": 1}) for label, x, y, z in body: msp.add_point((x, y, z), dxfattribs={"layer": "BODY"}) msp.add_text( label + "*"*(body.count((label, x, y, z))-1), dxfattribs={"layer": "POPISKY", "height": float(velikost)}).set_pos((x + 2, y + 2), align="LEFT") # Posun textu od bodu my_file = StringIO() out_file = BytesIO() doc.write(my_file, fmt="asc") print(my_file) out_file.write(my_file.getvalue().encode('utf-8')) return out_file if __name__ == '__main__': app.run(debug=False, port=5003, host='0.0.0.0')