-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathmain_force_pdf_generator.py
More file actions
456 lines (371 loc) · 14.3 KB
/
main_force_pdf_generator.py
File metadata and controls
456 lines (371 loc) · 14.3 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import os
import base64
import re
from datetime import datetime
import streamlit as st
import pandas as pd
def generate_main_force_markdown_report(analyzer, result):
"""生成主力选股Markdown格式的分析报告"""
# 获取当前时间
current_time = datetime.now().strftime("%Y年%m月%d日 %H:%M:%S")
# 获取分析参数
params = result.get('params', {})
start_date = params.get('start_date', 'N/A')
min_cap = params.get('min_market_cap', 50)
max_cap = params.get('max_market_cap', 5000)
max_change = params.get('max_range_change', 50)
markdown_content = f"""
# 主力选股AI分析报告
**生成时间**: {current_time}
---
## 📊 选股参数
| 项目 | 值 |
|------|-----|
| **起始日期** | {start_date} |
| **市值范围** | {min_cap}亿 - {max_cap}亿 |
| **最大涨跌幅** | {max_change}% |
| **初始数据量** | {result.get('total_fetched', 0)}只 |
| **筛选后数量** | {result.get('filtered_count', 0)}只 |
| **最终推荐** | {len(result.get('final_recommendations', []))}只 |
---
## 🤖 AI分析师团队报告
"""
# 添加资金流向分析
if hasattr(analyzer, 'fund_flow_analysis') and analyzer.fund_flow_analysis:
markdown_content += f"""
### 💰 资金流向分析师
{analyzer.fund_flow_analysis}
---
"""
# 添加行业板块分析
if hasattr(analyzer, 'industry_analysis') and analyzer.industry_analysis:
markdown_content += f"""
### 📊 行业板块及市场热点分析师
{analyzer.industry_analysis}
---
"""
# 添加财务基本面分析
if hasattr(analyzer, 'fundamental_analysis') and analyzer.fundamental_analysis:
markdown_content += f"""
### 📈 财务基本面分析师
{analyzer.fundamental_analysis}
---
"""
# 添加精选推荐
markdown_content += """
## ⭐ 精选推荐股票
"""
final_recommendations = result.get('final_recommendations', [])
if final_recommendations:
for rec in final_recommendations:
markdown_content += f"""
### 【第{rec['rank']}名】{rec['symbol']} - {rec['name']}
**推荐理由**:
{rec.get('reason', '暂无')}
**关键指标**:
"""
if 'stock_data' in rec:
stock_data = rec['stock_data']
markdown_content += f"""
- **所属行业**: {stock_data.get('industry', 'N/A')}
- **市值**: {stock_data.get('market_cap', 'N/A')}
- **主力资金流向**: {stock_data.get('main_fund_inflow', 'N/A')}
- **区间涨跌幅**: {stock_data.get('range_change', 'N/A')}%
- **市盈率**: {stock_data.get('pe_ratio', 'N/A')}
- **市净率**: {stock_data.get('pb_ratio', 'N/A')}
"""
if 'scores' in rec.get('stock_data', {}):
scores = rec['stock_data']['scores']
if scores:
markdown_content += "**能力评分**:\n"
for score_name, score_value in scores.items():
markdown_content += f"- {score_name}: {score_value}\n"
markdown_content += "\n"
markdown_content += "---\n\n"
else:
markdown_content += "暂无推荐股票\n\n---\n\n"
# 添加候选股票列表(前100名,按主力资金排序)
if analyzer and analyzer.raw_stocks is not None and not analyzer.raw_stocks.empty:
markdown_content += """
## 📋 候选股票完整列表(按主力资金净流入排序)
"""
# 获取主力资金列名
df = analyzer.raw_stocks
main_fund_col = None
main_fund_patterns = [
'区间主力资金流向', '区间主力资金净流入',
'主力资金流向', '主力资金净流入', '主力净流入'
]
for pattern in main_fund_patterns:
matching = [col for col in df.columns if pattern in col]
if matching:
main_fund_col = matching[0]
break
# 按主力资金排序
if main_fund_col:
df_sorted = df.copy()
df_sorted[main_fund_col] = pd.to_numeric(df_sorted[main_fund_col], errors='coerce')
df_sorted = df_sorted.sort_values(by=main_fund_col, ascending=False).head(100)
else:
df_sorted = df.head(100)
# 选择要显示的列
display_cols = []
if '股票代码' in df_sorted.columns:
display_cols.append('股票代码')
if '股票简称' in df_sorted.columns:
display_cols.append('股票简称')
# 行业
industry_cols = [col for col in df_sorted.columns if '行业' in col]
if industry_cols:
display_cols.append(industry_cols[0])
# 主力资金
if main_fund_col:
display_cols.append(main_fund_col)
# 涨跌幅
change_cols = [col for col in df_sorted.columns if '涨跌幅' in col]
if change_cols:
display_cols.append(change_cols[0])
# 市值、市盈率、市净率
for col_name in ['总市值', '市盈率', '市净率']:
matching_cols = [col for col in df_sorted.columns if col_name in col]
if matching_cols:
display_cols.append(matching_cols[0])
# 生成表格
if display_cols:
final_display_cols = [col for col in display_cols if col in df_sorted.columns]
markdown_content += "| 序号 | " + " | ".join(final_display_cols) + " |\n"
markdown_content += "|------|" + "|".join(['-----' for _ in final_display_cols]) + "|\n"
for idx, (_, row) in enumerate(df_sorted[final_display_cols].iterrows(), 1):
row_data = [str(idx)]
for col in final_display_cols:
value = row[col]
if pd.isna(value):
row_data.append('N/A')
else:
row_data.append(str(value))
markdown_content += "| " + " | ".join(row_data) + " |\n"
markdown_content += "\n"
# 添加免责声明
markdown_content += f"""
---
## 📝 免责声明
本报告由AI系统生成,仅供参考,不构成投资建议。投资有风险,入市需谨慎。请在做出投资决策前咨询专业的投资顾问。
---
*报告生成时间: {current_time}*
*主力选股AI分析系统 v1.0*
"""
return markdown_content
def generate_html_content(markdown_content):
"""将Markdown转换为HTML"""
html_content = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>主力选股AI分析报告</title>
<style>
body {{
font-family: 'Microsoft YaHei', Arial, sans-serif;
line-height: 1.6;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}}
.container {{
background-color: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}}
h1 {{
color: #2c3e50;
border-bottom: 3px solid #3498db;
padding-bottom: 10px;
}}
h2 {{
color: #34495e;
border-left: 4px solid #3498db;
padding-left: 15px;
margin-top: 30px;
}}
h3 {{
color: #2980b9;
margin-top: 25px;
}}
table {{
width: 100%;
border-collapse: collapse;
margin: 20px 0;
font-size: 14px;
}}
th, td {{
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}}
th {{
background-color: #3498db;
color: white;
font-weight: bold;
}}
tr:nth-child(even) {{
background-color: #f9f9f9;
}}
tr:hover {{
background-color: #f0f0f0;
}}
.disclaimer {{
background-color: #fff3cd;
border: 1px solid #ffeaa7;
border-radius: 5px;
padding: 15px;
margin-top: 30px;
}}
.footer {{
text-align: center;
margin-top: 30px;
color: #7f8c8d;
font-style: italic;
}}
hr {{
border: none;
height: 2px;
background-color: #ecf0f1;
margin: 20px 0;
}}
strong {{
color: #2c3e50;
}}
ul, ol {{
margin: 10px 0;
padding-left: 30px;
}}
li {{
margin: 5px 0;
}}
</style>
</head>
<body>
<div class="container">
"""
# 简单的Markdown到HTML转换
html_body = markdown_content
html_body = html_body.replace('\n# ', '\n<h1>').replace('\n## ', '\n<h2>').replace('\n### ', '\n<h3>')
html_body = html_body.replace('# ', '<h1>').replace('## ', '<h2>').replace('### ', '<h3>')
html_body = html_body.replace('\n---\n', '\n<hr>\n')
# 处理粗体文本
html_body = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', html_body)
# 处理表格
lines = html_body.split('\n')
in_table = False
processed_lines = []
for line in lines:
if '|' in line and not in_table and line.strip().startswith('|'):
processed_lines.append('<table>')
in_table = True
cells = [cell.strip() for cell in line.split('|')[1:-1]]
processed_lines.append('<tr>')
for cell in cells:
processed_lines.append(f'<th>{cell}</th>')
processed_lines.append('</tr>')
elif '|' in line and in_table:
if '---' not in line:
cells = [cell.strip() for cell in line.split('|')[1:-1]]
processed_lines.append('<tr>')
for cell in cells:
processed_lines.append(f'<td>{cell}</td>')
processed_lines.append('</tr>')
elif in_table and '|' not in line:
processed_lines.append('</table>')
in_table = False
processed_lines.append(line)
else:
processed_lines.append(line)
if in_table:
processed_lines.append('</table>')
html_body = '\n'.join(processed_lines)
# 处理列表
html_body = re.sub(r'\n- (.*)', r'\n<li>\1</li>', html_body)
html_body = re.sub(r'(<li>.*</li>)\n(?!<li>)', r'<ul>\1</ul>\n', html_body)
html_body = re.sub(r'(<li>.*</li>\n)+', lambda m: '<ul>\n' + m.group(0) + '</ul>\n', html_body)
# 处理换行
html_body = html_body.replace('\n\n', '</p><p>')
html_body = '<p>' + html_body + '</p>'
html_content += html_body
html_content += """
</div>
</body>
</html>
"""
return html_content
def create_download_link(content, filename, link_text):
"""创建下载链接"""
b64 = base64.b64encode(content.encode()).decode()
href = f'<a href="data:text/markdown;base64,{b64}" download="{filename}" style="display: inline-block; padding: 10px 20px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 5px; margin: 5px;">{link_text}</a>'
return href
def create_html_download_link(content, filename, link_text):
"""创建HTML下载链接"""
b64 = base64.b64encode(content.encode('utf-8')).decode()
href = f'<a href="data:text/html;base64,{b64}" download="{filename}" style="display: inline-block; padding: 10px 20px; background-color: #2196F3; color: white; text-decoration: none; border-radius: 5px; margin: 5px;">{link_text}</a>'
return href
def display_report_download_section(analyzer, result):
"""显示报告下载区域"""
st.markdown("---")
st.markdown("### 📥 下载分析报告")
col1, col2 = st.columns(2)
with col1:
st.markdown("#### 📄 Markdown格式")
st.caption("适合编辑和进一步处理")
# 生成Markdown报告
markdown_content = generate_main_force_markdown_report(analyzer, result)
# 生成文件名
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
md_filename = f"主力选股分析报告_{timestamp}.md"
# 创建下载链接
md_link = create_download_link(markdown_content, md_filename, "📥 下载Markdown报告")
st.markdown(md_link, unsafe_allow_html=True)
# 显示预览
with st.expander("👀 预览Markdown内容"):
st.code(markdown_content[:2000] + "..." if len(markdown_content) > 2000 else markdown_content)
with col2:
st.markdown("#### 🌐 HTML格式")
st.caption("可在浏览器中打开查看")
# 生成HTML报告
html_content = generate_html_content(markdown_content)
# 生成文件名
html_filename = f"主力选股分析报告_{timestamp}.html"
# 创建下载链接
html_link = create_html_download_link(html_content, html_filename, "📥 下载HTML报告")
st.markdown(html_link, unsafe_allow_html=True)
# 显示说明
st.info("💡 HTML报告可以直接在浏览器中打开,格式美观易读")
# 添加CSV下载(候选股票列表)
if analyzer and analyzer.raw_stocks is not None and not analyzer.raw_stocks.empty:
st.markdown("---")
st.markdown("#### 📊 候选股票数据")
# 按主力资金排序
df = analyzer.raw_stocks.copy()
main_fund_col = None
main_fund_patterns = [
'区间主力资金流向', '区间主力资金净流入',
'主力资金流向', '主力资金净流入', '主力净流入'
]
for pattern in main_fund_patterns:
matching = [col for col in df.columns if pattern in col]
if matching:
main_fund_col = matching[0]
break
if main_fund_col:
df[main_fund_col] = pd.to_numeric(df[main_fund_col], errors='coerce')
df = df.sort_values(by=main_fund_col, ascending=False)
# 导出为CSV
csv = df.to_csv(index=False, encoding='utf-8-sig')
csv_filename = f"主力选股候选列表_{timestamp}.csv"
st.download_button(
label="📥 下载候选股票CSV",
data=csv,
file_name=csv_filename,
mime="text/csv",
width='content'
)