-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest1.py
More file actions
149 lines (119 loc) · 4.68 KB
/
test1.py
File metadata and controls
149 lines (119 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from flask import Flask, request, jsonify
import pandas as pd
import os
from fpdf import FPDF
import matplotlib.pyplot as plt
app = Flask(__name__)
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file and file.filename.endswith('.xlsx'):
file_path = os.path.join('uploads', file.filename)
file.save(file_path)
xls = pd.ExcelFile(file_path)
return jsonify({
'file_path': file_path,
'sheet_names': xls.sheet_names,
'number_of_sheets': len(xls.sheet_names)
})
else:
return jsonify({'error': 'Invalid file type'}), 400
@app.route('/build-report', methods=['POST'])
def process_data():
data = request.json
if 'operations' not in data:
return jsonify({'error': 'Missing "operations" key in request data'}), 400
operations = data['operations']
file_path = data.get('file_path', '')
if not file_path:
return jsonify({'error': 'Missing "file_path" key in request data'}), 400
report = {}
xls = pd.ExcelFile(file_path)
available_sheets = xls.sheet_names
for operation_info in operations:
sheet_name = operation_info.get('sheet_name')
if sheet_name not in available_sheets:
return jsonify({'error': f'Sheet {sheet_name} not found'}), 400
operation = operation_info.get('operation')
columns = operation_info.get('columns', [])
df = pd.read_excel(file_path, sheet_name=sheet_name)
missing_columns = [col for col in columns if col not in df.columns]
if missing_columns:
return jsonify({'error': f'Missing columns in sheet {sheet_name}: {missing_columns}'}), 400
if operation == 'sum':
result = df[columns].sum().to_dict()
elif operation == 'average':
result = df[columns].mean().to_dict()
else:
return jsonify({'error': 'Invalid operation'}), 400
report[sheet_name] = result
return jsonify(report)
@app.route('/generate_pdf', methods=['POST'])
def generate_pdf():
report = request.json
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
for sheet_name, data in report.items():
pdf.cell(200, 10, txt=sheet_name, ln=True)
for col, val in data.items():
pdf.cell(200, 10, txt=f"{col}: {val}", ln=True)
pdf_output_path = 'report.pdf'
pdf.output(pdf_output_path)
return jsonify({'pdf_path': pdf_output_path})
@app.route('/plot', methods=['POST'])
def plot_graph():
data = request.json
sums = {sheet: sum(val.values()) for sheet, val in data.items()}
sheets = list(sums.keys())
values = list(sums.values())
plt.bar(sheets, values)
plt.xlabel('Sheet Names')
plt.ylabel('Sum')
plt.title('Sum of Each Sheet')
plt.savefig('sheet_sums.png')
return jsonify({'graph_path': 'sheet_sums.png'})
@app.route('/report', methods=['POST'])
def generate_report():
data = request.json
file_path = data['file_path']
sheets_info = data['sheets'] # List of dicts with sheet name, operation, and columns
report = {}
for sheet_info in sheets_info:
sheet_name = sheet_info['sheet_name']
operation = sheet_info['operation']
columns = sheet_info['columns']
# Load the sheet
df = pd.read_excel(file_path, sheet_name=sheet_name)
if operation == 'sum':
result = df[columns].sum().to_dict()
elif operation == 'average':
result = df[columns].mean().to_dict()
else:
return jsonify({'error': 'Invalid operation'}), 400
report[sheet_name] = result
return jsonify(report)
@app.route('/generate_detailed_pdf', methods=['POST'])
def generate_detailed_pdf():
report = request.json
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(200, 10, txt="Report Details", ln=True)
for sheet_name, data in report.items():
pdf.cell(200, 10, txt=sheet_name, ln=True)
for col, val in data.items():
pdf.cell(200, 10, txt=f"{col}: {val}", ln=True)
# Adding graphs
pdf.add_page()
pdf.cell(200, 10, txt="Graphs", ln=True)
pdf.image('sheet_sums.png', x=10, y=30, w=180)
pdf_output_path = 'detailed_report.pdf'
pdf.output(pdf_output_path)
return jsonify({'pdf_path': pdf_output_path})
if __name__ == '__main__':
app.run(debug=True)