70 lines
2.8 KiB
Python
70 lines
2.8 KiB
Python
from adata import stock
|
||
import pandas as pd
|
||
|
||
# 显示所有列
|
||
pd.set_option('display.max_columns', None)
|
||
# 显示所有行
|
||
pd.set_option('display.max_rows', None)
|
||
# 显示单元格完整内容(不截断长文本)
|
||
pd.set_option('display.max_colwidth', None)
|
||
# 不自动折行
|
||
pd.set_option('display.expand_frame_repr', False)
|
||
|
||
|
||
def get_fina_info(stock_code="600699"):
|
||
|
||
df = stock.finance.get_core_index(stock_code=stock_code)
|
||
|
||
data = df[['report_date', 'report_type', 'total_rev', 'total_rev_yoy_gr','net_profit_attr_sh', 'net_profit_yoy_gr',
|
||
'non_gaap_net_profit', 'non_gaap_net_profit_yoy_gr', 'roe_wtd', 'gross_margin', 'net_margin', 'quick_ratio',
|
||
'inv_turn_rate', 'acct_recv_turn_rate']]
|
||
|
||
last_type = data['report_type'].to_list()[0]
|
||
|
||
recent_df = data[data.report_type == last_type].iloc[:2,:].reset_index(drop=True)
|
||
|
||
recent_df.rename(columns={"report_date": "报告日期",
|
||
"report_type": "报告类型",
|
||
"total_rev":"营业总收入",
|
||
"total_rev_yoy_gr":"营业总收入同比增长率",
|
||
"net_profit_attr_sh":"归母净利润",
|
||
"net_profit_yoy_gr":"归母净利润同比增长率",
|
||
"non_gaap_net_profit":"扣非净利润",
|
||
"non_gaap_net_profit_yoy_gr":"扣非净利润同比增长率",
|
||
"roe_wtd":"加权净资产收益率",
|
||
"gross_margin":"销售毛利率",
|
||
"net_margin":"销售净利率",
|
||
"quick_ratio":"速动比率",
|
||
"inv_turn_rate":"存货周转率",
|
||
"acct_recv_turn_rate":"应收账款周转率"
|
||
}, inplace=True)
|
||
|
||
formatted_lines = []
|
||
for _, row in recent_df.iterrows():
|
||
line_items = []
|
||
for col, val in zip(recent_df.columns, row):
|
||
# 判断是否为数字类型
|
||
if isinstance(val, (int, float)):
|
||
# 如果列名包含"率",则格式化为保留两位小数并加上 %
|
||
if "率" in col:
|
||
formatted_val = f"{val:.2f}%"
|
||
else:
|
||
# 其他数字:格式化为带千分位、保留两位小数
|
||
formatted_val = f"{val:,.2f}"
|
||
else:
|
||
# 非数字类型(如日期、文本)直接转为字符串
|
||
formatted_val = str(val)
|
||
|
||
line_items.append(f"{col}:{formatted_val}")
|
||
|
||
# 将这一期的所有字段用换行符拼接
|
||
formatted_lines.append("\n".join(line_items))
|
||
|
||
# 3. 组装最终报告文本
|
||
final_report = f"近两期{last_type}主要财务数据如下:\n" + "\n\n".join(formatted_lines)
|
||
|
||
return final_report
|
||
|
||
|
||
if __name__ == "__main__":
|
||
print(get_fina_info(stock_code="600105")) |