File size: 1,973 Bytes
c3a7e1f b556bb6 c3a7e1f |
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 |
from flask import Flask, request, send_file
import pandas as pd
import re
from collections import Counter
from werkzeug.utils import secure_filename
import os
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
OUTPUT_FOLDER = 'outputs'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
@app.route('/')
def index():
return '''
<h1>์์
ํค์๋ ์ถ์ถ๊ธฐ</h1>
<form action="/process" method="post" enctype="multipart/form-data">
<input type="file" name="file" accept=".xlsx"/>
<button type="submit">์
๋ก๋ ๋ฐ ์ฒ๋ฆฌ</button>
</form>
'''
@app.route('/process', methods=['POST'])
def process():
file = request.files['file']
filename = secure_filename(file.filename)
filepath = os.path.join(UPLOAD_FOLDER, filename)
file.save(filepath)
# ์์
๋ฐ์ดํฐ ์ฝ๊ธฐ
df = pd.read_excel(filepath, engine='openpyxl')
# D4:D์ด์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
data = df.iloc[3:, 3].dropna().astype(str)
# ํค์๋ ์ฒ๋ฆฌ
keywords = []
for text in data:
text = re.sub(r'[^\w\s]', '', text) # ํน์๋ฌธ์ ์ ๊ฑฐ
keywords.extend(text.split())
# ํค์๋ ๋น๋ ๊ณ์ฐ
keyword_counts = Counter(keywords)
sorted_keywords = sorted(keyword_counts.items(), key=lambda x: x[1], reverse=True)
# ๊ฒฐ๊ณผ ๋ฐ์ดํฐํ๋ ์ ์์ฑ
result_df = pd.DataFrame(sorted_keywords, columns=['ํค์๋', '๋น๋'])
# ๊ฒฐ๊ณผ ์์
๋ก ์ ์ฅ
output_path = os.path.join(OUTPUT_FOLDER, 'result.xlsx')
with pd.ExcelWriter(output_path, engine='openpyxl') as writer:
result_df.to_excel(writer, index=False, startrow=4, startcol=0, header=False)
sheet = writer.sheets['Sheet1']
sheet.cell(row=4, column=1).value = "A5์
๋ช
"
sheet.cell(row=4, column=2).value = "B5์
๋ช
"
return send_file(output_path, as_attachment=True, download_name='result.xlsx')
if __name__ == '__main__':
app.run(debug=True)
|