118 lines
4.3 KiB
Python
118 lines
4.3 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)
|
||
|
||
print(df.columns)
|
||
|
||
last_type = df['report_type'].to_list()[0]
|
||
|
||
last_data = df[df.report_type == last_type].iloc[:2,:]
|
||
|
||
# 字典定义:英文指标名 -> 中文财务指标名
|
||
column_mapping = {
|
||
# 基础与披露信息
|
||
'stock_code': '股票代码',
|
||
'short_name': '股票简称',
|
||
'report_date': '报告期',
|
||
'report_type': '报表类型',
|
||
'notice_date': '公告日期',
|
||
|
||
# 每股指标
|
||
'basic_eps': '基本每股收益',
|
||
'diluted_eps': '稀释每股收益',
|
||
'non_gaap_eps': '扣非每股收益',
|
||
'net_asset_ps': '每股净资产',
|
||
'cap_reserve_ps': '每股公积金',
|
||
'undist_profit_ps': '每股未分配利润',
|
||
'oper_cf_ps': '每股经营现金流',
|
||
|
||
# 规模与利润指标 (元)
|
||
'total_rev': '营业总收入',
|
||
'gross_profit': '毛利润',
|
||
'net_profit_attr_sh': '归母净利润',
|
||
'non_gaap_net_profit': '扣非归母净利润',
|
||
|
||
# 增长率指标 (同比 YoY / 环比 QoQ)
|
||
'total_rev_yoy_gr': '营业总收入同比增长率',
|
||
'net_profit_yoy_gr': '归母净利润同比增长率',
|
||
'non_gaap_net_profit_yoy_gr': '扣非归母净利润同比增长率',
|
||
'total_rev_qoq_gr': '营业总收入环比增长率',
|
||
'net_profit_qoq_gr': '归母净利润环比增长率',
|
||
'non_gaap_net_profit_qoq_gr': '扣非归母净利润环比增长率',
|
||
|
||
# 盈利能力与收益率
|
||
'roe_wtd': '加权净资产收益率(ROE)',
|
||
'roe_non_gaap_wtd': '扣非加权净资产收益率(ROE)',
|
||
'roa_wtd': '加权总资产收益率(ROA)',
|
||
'gross_margin': '销售毛利率',
|
||
'net_margin': '销售净利率',
|
||
|
||
# 现金流与收入质量比率
|
||
'adv_receipts_to_rev': '预收款及合同负债占营收比',
|
||
'net_cf_sales_to_rev': '销售收现率(销售现金流/营收)',
|
||
'oper_cf_to_rev': '经营现金净流量占营收比',
|
||
'eff_tax_rate': '实际有效税率',
|
||
|
||
# 偿债能力与财务杠杆
|
||
'curr_ratio': '流动比率',
|
||
'quick_ratio': '速动比率',
|
||
'cash_flow_ratio': '现金流量比率',
|
||
'asset_liab_ratio': '资产负债率',
|
||
'equity_multiplier': '权益乘数',
|
||
'equity_ratio': '产权比率',
|
||
|
||
# 运营效率与周转率/天数
|
||
'total_asset_turn_days': '总资产周转天数',
|
||
'inv_turn_days': '存货周转天数',
|
||
'acct_recv_turn_days': '应收账款周转天数',
|
||
'total_asset_turn_rate': '总资产周转率',
|
||
'inv_turn_rate': '存货周转率',
|
||
'acct_recv_turn_rate': '应收账款周转率'
|
||
}
|
||
|
||
# 假设你的原始数据存储在 df 中,使用 rename 修改列名
|
||
# inplace=True 表示直接在原 DataFrame 上修改
|
||
last_data.rename(columns=column_mapping, inplace=True)
|
||
|
||
formatted_lines = []
|
||
for _, row in last_data.iterrows():
|
||
line_items = []
|
||
for col, val in zip(last_data.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")) |