117 lines
3.9 KiB
Python
117 lines
3.9 KiB
Python
from flask import Flask, request, render_template, send_file, make_response
|
|
from io import BytesIO, StringIO
|
|
import ezdxf
|
|
import openpyxl
|
|
import os
|
|
import sys
|
|
import pandas as pd
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
def upload_file():
|
|
if request.method == 'POST':
|
|
file = request.files['file']
|
|
if request.form['btn'] == 'dxf':
|
|
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']
|
|
nazev_souboru = request.form['output_file']
|
|
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= nazev_souboru+".dxf" or 'hotovson.dxf',
|
|
mimetype='application/octet-stream'
|
|
)
|
|
else:
|
|
return render_template('upload_form.html', no_file=True)
|
|
else:
|
|
# Generate the download file
|
|
file_data = BytesIO(b"necum")
|
|
file_name = request.form['excel_output_file']
|
|
|
|
|
|
df = pd.read_excel(file, sheet_name='List1')
|
|
|
|
|
|
df_deduplicated = df.drop_duplicates(subset=['Název'], keep='first')
|
|
|
|
df_final = pd.concat([df, df_deduplicated]).sort_values(by=['x', 'y'], ascending=request.form.get('select')== '1')
|
|
df_final = df_final['Název'].reset_index(drop=True)
|
|
|
|
|
|
df_final.to_excel(file_data, sheet_name='List1', index=False, header=False)
|
|
file_data.seek(0)
|
|
|
|
return send_file(
|
|
path_or_file=file_data,
|
|
as_attachment=True,
|
|
download_name= file_name+".xlsx" or 'hotovson.xlsx',
|
|
mimetype='application/octet-stream'
|
|
)
|
|
|
|
return render_template('upload_form.html')
|
|
|
|
@app.route('/excel_labels', methods=['GET', 'POST'])
|
|
def excel_labels():
|
|
|
|
# Generate the download file
|
|
file_data = BytesIO(b"necum")
|
|
file_name = 'download_file.xlsx'
|
|
|
|
response = make_response(send_file(file_data, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'))
|
|
response.headers['Content-Disposition'] = f'attachment; {file_name}'
|
|
return response
|
|
|
|
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=True, port=5003, host='0.0.0.0') |