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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, send_file, redirect
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
from threading import Thread
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
UPLOAD_FOLDER = 'uploads'
|
| 10 |
+
ALLOWED_EXTENSIONS = {'xlsx'}
|
| 11 |
+
CLEANUP_INTERVAL = 300 # 5分钟
|
| 12 |
+
|
| 13 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 14 |
+
|
| 15 |
+
def cleanup_uploads():
|
| 16 |
+
"""定时清理uploads文件夹"""
|
| 17 |
+
while True:
|
| 18 |
+
time.sleep(CLEANUP_INTERVAL)
|
| 19 |
+
try:
|
| 20 |
+
for filename in os.listdir(app.config['UPLOAD_FOLDER']):
|
| 21 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 22 |
+
try:
|
| 23 |
+
if os.path.isfile(file_path) or os.path.islink(file_path):
|
| 24 |
+
os.unlink(file_path)
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f'删除文件 {file_path} 失败: {e}')
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f'清理uploads文件夹失败: {e}')
|
| 29 |
+
|
| 30 |
+
# 启动清理线程
|
| 31 |
+
cleanup_thread = Thread(target=cleanup_uploads)
|
| 32 |
+
cleanup_thread.daemon = True
|
| 33 |
+
cleanup_thread.start()
|
| 34 |
+
|
| 35 |
+
def allowed_file(filename):
|
| 36 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 37 |
+
|
| 38 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 39 |
+
def index():
|
| 40 |
+
if request.method == 'POST':
|
| 41 |
+
if 'file' not in request.files:
|
| 42 |
+
return "没有选择文件"
|
| 43 |
+
|
| 44 |
+
file = request.files['file']
|
| 45 |
+
if file.filename == '':
|
| 46 |
+
return "没有选择文件"
|
| 47 |
+
|
| 48 |
+
if file and allowed_file(file.filename):
|
| 49 |
+
# 确保上传目录存在
|
| 50 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 51 |
+
|
| 52 |
+
# 生成安全的文件名
|
| 53 |
+
upload_dir = os.path.abspath(app.config['UPLOAD_FOLDER'])
|
| 54 |
+
os.makedirs(upload_dir, exist_ok=True)
|
| 55 |
+
filename = os.path.join(upload_dir, file.filename)
|
| 56 |
+
file.save(filename)
|
| 57 |
+
|
| 58 |
+
# 处理Excel文件
|
| 59 |
+
df = pd.read_excel(filename)
|
| 60 |
+
df_deduplicated = df.drop_duplicates()
|
| 61 |
+
|
| 62 |
+
# 生成带时间戳的输出文件名
|
| 63 |
+
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 64 |
+
base_name, ext = os.path.splitext(file.filename)
|
| 65 |
+
output_filename = os.path.join(upload_dir, f'{base_name}_{timestamp}{ext}')
|
| 66 |
+
df_deduplicated.to_excel(output_filename, index=False)
|
| 67 |
+
|
| 68 |
+
# 确保文件已写入
|
| 69 |
+
if not os.path.exists(output_filename):
|
| 70 |
+
raise FileNotFoundError(f"无法创建文件: {output_filename}")
|
| 71 |
+
|
| 72 |
+
return f'''<!doctype html>
|
| 73 |
+
<html>
|
| 74 |
+
<head>
|
| 75 |
+
<meta charset="UTF-8">
|
| 76 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 77 |
+
<title>去重完成</title>
|
| 78 |
+
<style>
|
| 79 |
+
body {{
|
| 80 |
+
font-family: Arial, sans-serif;
|
| 81 |
+
margin: 20px;
|
| 82 |
+
padding: 0;
|
| 83 |
+
max-width: 800px;
|
| 84 |
+
margin: 0 auto;
|
| 85 |
+
}}
|
| 86 |
+
.result-container {{
|
| 87 |
+
margin-top: 30px;
|
| 88 |
+
padding: 20px;
|
| 89 |
+
background: #ecf0f1;
|
| 90 |
+
border-radius: 8px;
|
| 91 |
+
text-align: center;
|
| 92 |
+
}}
|
| 93 |
+
.btn {{
|
| 94 |
+
display: inline-block;
|
| 95 |
+
padding: 10px 20px;
|
| 96 |
+
background: #3498db;
|
| 97 |
+
color: white;
|
| 98 |
+
text-decoration: none;
|
| 99 |
+
border-radius: 4px;
|
| 100 |
+
margin: 10px;
|
| 101 |
+
}}
|
| 102 |
+
.btn-danger {{
|
| 103 |
+
background: #e74c3c;
|
| 104 |
+
}}
|
| 105 |
+
.btn:hover {{
|
| 106 |
+
opacity: 0.9;
|
| 107 |
+
}}
|
| 108 |
+
</style>
|
| 109 |
+
</head>
|
| 110 |
+
<body>
|
| 111 |
+
<h1>去重完成</h1>
|
| 112 |
+
<div class="result-container">
|
| 113 |
+
<p>文件去重处理成功!</p>
|
| 114 |
+
<button onclick="clearCache()" class="btn btn-danger">清理缓存</button>
|
| 115 |
+
<script>
|
| 116 |
+
function clearCache() {{
|
| 117 |
+
fetch('/clear_cache', {{ method: 'POST' }})
|
| 118 |
+
.then(() => window.location.href = '/')
|
| 119 |
+
.catch(err => console.error(err));
|
| 120 |
+
}}
|
| 121 |
+
</script>
|
| 122 |
+
<a href="/download/{os.path.basename(output_filename)}" class="btn">下载去重后的文件</a>
|
| 123 |
+
</div>
|
| 124 |
+
</body>
|
| 125 |
+
</html>'''
|
| 126 |
+
|
| 127 |
+
return '''
|
| 128 |
+
<!doctype html>
|
| 129 |
+
<html>
|
| 130 |
+
<head>
|
| 131 |
+
<meta charset="UTF-8">
|
| 132 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 133 |
+
<title>Excel去重工具</title>
|
| 134 |
+
<style>
|
| 135 |
+
body {
|
| 136 |
+
font-family: Arial, sans-serif;
|
| 137 |
+
margin: 20px;
|
| 138 |
+
padding: 0;
|
| 139 |
+
max-width: 800px;
|
| 140 |
+
margin: 0 auto;
|
| 141 |
+
}
|
| 142 |
+
h1 {
|
| 143 |
+
color: #2c3e50;
|
| 144 |
+
text-align: center;
|
| 145 |
+
margin: 30px 0;
|
| 146 |
+
}
|
| 147 |
+
.upload-form {
|
| 148 |
+
background: #f5f5f5;
|
| 149 |
+
padding: 20px;
|
| 150 |
+
border-radius: 8px;
|
| 151 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 152 |
+
}
|
| 153 |
+
.upload-form input[type="file"] {
|
| 154 |
+
margin: 10px 0;
|
| 155 |
+
padding: 10px;
|
| 156 |
+
width: 100%;
|
| 157 |
+
box-sizing: border-box;
|
| 158 |
+
}
|
| 159 |
+
.upload-form input[type="submit"] {
|
| 160 |
+
background: #3498db;
|
| 161 |
+
color: white;
|
| 162 |
+
border: none;
|
| 163 |
+
padding: 12px 24px;
|
| 164 |
+
border-radius: 4px;
|
| 165 |
+
cursor: pointer;
|
| 166 |
+
width: 100%;
|
| 167 |
+
font-size: 16px;
|
| 168 |
+
}
|
| 169 |
+
.upload-form input[type="submit"]:hover {
|
| 170 |
+
background: #2980b9;
|
| 171 |
+
}
|
| 172 |
+
.result-container {
|
| 173 |
+
margin-top: 30px;
|
| 174 |
+
padding: 20px;
|
| 175 |
+
background: #ecf0f1;
|
| 176 |
+
border-radius: 8px;
|
| 177 |
+
}
|
| 178 |
+
.btn {
|
| 179 |
+
display: inline-block;
|
| 180 |
+
padding: 10px 20px;
|
| 181 |
+
background: #e74c3c;
|
| 182 |
+
color: white;
|
| 183 |
+
text-decoration: none;
|
| 184 |
+
border-radius: 4px;
|
| 185 |
+
margin: 10px 0;
|
| 186 |
+
}
|
| 187 |
+
.btn:hover {
|
| 188 |
+
background: #c0392b;
|
| 189 |
+
}
|
| 190 |
+
@media (max-width: 600px) {
|
| 191 |
+
h1 {
|
| 192 |
+
font-size: 24px;
|
| 193 |
+
}
|
| 194 |
+
.upload-form {
|
| 195 |
+
padding: 15px;
|
| 196 |
+
}
|
| 197 |
+
}
|
| 198 |
+
</style>
|
| 199 |
+
</head>
|
| 200 |
+
<body>
|
| 201 |
+
<h1>Excel文件去重工具</h1>
|
| 202 |
+
<div class="upload-form">
|
| 203 |
+
<form method="post" enctype="multipart/form-data">
|
| 204 |
+
<input type="file" name="file" accept=".xlsx" required>
|
| 205 |
+
<input type="submit" value="开始去重">
|
| 206 |
+
</form>
|
| 207 |
+
</div>
|
| 208 |
+
</body>
|
| 209 |
+
</html>
|
| 210 |
+
'''
|
| 211 |
+
|
| 212 |
+
@app.route('/download/<filename>')
|
| 213 |
+
def download_file(filename):
|
| 214 |
+
try:
|
| 215 |
+
upload_dir = os.path.abspath(app.config['UPLOAD_FOLDER'])
|
| 216 |
+
path = os.path.join(upload_dir, filename)
|
| 217 |
+
if not os.path.exists(path):
|
| 218 |
+
raise FileNotFoundError(f"文件 {filename} 不存在")
|
| 219 |
+
return send_file(path, as_attachment=True)
|
| 220 |
+
except Exception as e:
|
| 221 |
+
return f"下载文件失败: {str(e)}", 500
|
| 222 |
+
|
| 223 |
+
@app.route('/clear_cache', methods=['POST'])
|
| 224 |
+
def clear_cache():
|
| 225 |
+
try:
|
| 226 |
+
for filename in os.listdir(app.config['UPLOAD_FOLDER']):
|
| 227 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 228 |
+
try:
|
| 229 |
+
if os.path.isfile(file_path) or os.path.islink(file_path):
|
| 230 |
+
os.unlink(file_path)
|
| 231 |
+
except Exception as e:
|
| 232 |
+
print(f'删除文件 {file_path} 失败: {e}')
|
| 233 |
+
return '''
|
| 234 |
+
<!doctype html>
|
| 235 |
+
<html>
|
| 236 |
+
<head>
|
| 237 |
+
<meta charset="UTF-8">
|
| 238 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 239 |
+
<title>缓存已清理</title>
|
| 240 |
+
<style>
|
| 241 |
+
body {
|
| 242 |
+
font-family: Arial, sans-serif;
|
| 243 |
+
text-align: center;
|
| 244 |
+
margin-top: 50px;
|
| 245 |
+
}
|
| 246 |
+
.countdown {
|
| 247 |
+
font-size: 24px;
|
| 248 |
+
color: #2c3e50;
|
| 249 |
+
margin: 20px 0;
|
| 250 |
+
}
|
| 251 |
+
</style>
|
| 252 |
+
<script>
|
| 253 |
+
let count = 3;
|
| 254 |
+
const countdown = document.getElementById('countdown');
|
| 255 |
+
setInterval(() => {
|
| 256 |
+
count--;
|
| 257 |
+
countdown.innerText = count;
|
| 258 |
+
if (count === 0) {
|
| 259 |
+
window.location.href = '/';
|
| 260 |
+
}
|
| 261 |
+
}, 1000);
|
| 262 |
+
</script>
|
| 263 |
+
</head>
|
| 264 |
+
<body>
|
| 265 |
+
<h1>缓存已成功清理</h1>
|
| 266 |
+
<div class="countdown">
|
| 267 |
+
将在 <span id="countdown">3</span> 秒后返回首页
|
| 268 |
+
</div>
|
| 269 |
+
</body>
|
| 270 |
+
</html>
|
| 271 |
+
'''
|
| 272 |
+
except Exception as e:
|
| 273 |
+
return f'清理缓存失败: {e}'
|
| 274 |
+
|
| 275 |
+
if __name__ == '__main__':
|
| 276 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 277 |
+
app.run(host='0.0.0.0', port=5000)
|