当前位置:网站首页>码力十足学量化|如何在财务报告寻找合适的财务公告
码力十足学量化|如何在财务报告寻找合适的财务公告
2022-07-01 06:18:00 【码力十足学量化】
做投资,我们经常要阅读公告,如何对公告进行解读呢?本文将遍历一个文件夹里的所有的理财公告
PDF文件,通过PDF文本解析对理财公告进行深度筛选,获取符合要求的公告内容。
1.首先遍历文件夹里所有的PDF文件,拼接出需要的pdf的文件位置。然后把他们放在一个列表里面,方便之后调用。
import os
import pdfplumber
path= r"C:\Users\86186\PycharmProjects\online\spider\requests\财务报表" # 财务报表储存路径
# 寻找文件夹下所有pdf文件路径
file_list=[]
for files in os.walk(path): # 遍历路径下所有文件夹
for file in files[2]: # 遍历路径下所有文件
if os.path.splitext(file)[1]=='.pdf' or os.path.splitext(file)[1]=='.PDF': # 检查文件后缀名
file_list.append(path+"\\"+file) # 拼接文件路径
print(file_list)
2.遍历所有pdf里面的内容,然后用pdfplumber函数获取文本里面的内容,将所有的文本内容拼接到一起。
# PDF文本解析和筛选出正文中的内容
pdf_all=[]
for i in range(len(file_list)):
pdf=pdfplumber.open(file_list[i]) # 打开每个pdf文件
pages=pdf.pages
text_all=[]
for page in pages: # 遍历每一页的信息
text=page.extract_text() # 提取当前页的文本内容
text_all.append(text) # 将每一页的内容汇集到一起
text_all="".join(text_all) # 把列表转换为字符串
pdf.close()
3.对遍历后得到的文本内容进行分析,这里写的关键字筛选,只有当PDF文件里含有“自有”,“议案”,“理财”和“现金管理”
,将这些文件筛选出来。这里大家可以通过自然语言和机器学习来进行更有深度的分析。
if ("增持"in text_all) or ("现金管理" in text_all) or ("理财" in text_all):
pdf_all.append(file_list[i])
print(pdf_all) # 打印筛选出的pdf文件
4.筛选出来后,将筛选出来的PDF文件执行移动操作,创建一个新的文件路径,然后将筛选出来的PDF文件移动到新的文件夹里。
# 移动筛选后pdf文件
for pdf_i in pdf_all:
new_path=r"C:\\Users\\86186\\PycharmProjects\\online\\spider\\requests\\筛选后的文件夹\\"+pdf_i.split('\\')[-1]
os.rename(pdf_i,new_path) # 执行文件的移动操作
print("PDF文本解析及筛选完毕!")
5.完成代码
import os
import pdfplumber
path= r"C:\Users\86186\PycharmProjects\online\spider\requests\财务报表" # 财务报表储存路径
# 寻找文件夹下所有pdf文件路径
file_list=[]
for files in os.walk(path): # 遍历路径下所有文件夹
for file in files[2]: # 遍历路径下所有文件
if os.path.splitext(file)[1]=='.pdf' or os.path.splitext(file)[1]=='.PDF': # 检查文件后缀名
file_list.append(path+"\\"+file) # 拼接文件路径
print(file_list)
# PDF文本解析和筛选出正文中的内容
pdf_all=[]
for i in range(len(file_list)):
pdf=pdfplumber.open(file_list[i]) # 打开每个pdf文件
pages=pdf.pages
text_all=[]
for page in pages: # 遍历每一页的信息
text=page.extract_text() # 提取当前页的文本内容
text_all.append(text) # 将每一页的内容汇集到一起
text_all="".join(text_all) # 把列表转换为字符串
pdf.close()
# 筛选正文中的内容
if ("增持"in text_all) or ("现金管理" in text_all) or ("理财" in text_all):
pdf_all.append(file_list[i])
print(pdf_all) # 打印筛选出的pdf文件
# 移动筛选后pdf文件
for pdf_i in pdf_all:
new_path=r"C:\\Users\\86186\\PycharmProjects\\online\\spider\\requests\\筛选后的文件夹\\"+pdf_i.split('\\')[-1]
os.rename(pdf_i,new_path) # 执行文件的移动操作
print("PDF文本解析及筛选完毕!")
边栏推荐
猜你喜欢

How did ManageEngine Zhuohao achieve the goal of being selected into Gartner Magic Quadrant for four consecutive years?

Database problems, how to optimize Oracle SQL query statements faster and more efficient

HCM Beginner (IV) - time

数据库产生死锁了请问一下有没有解决办法

记磁盘扇区损坏导致的Mysql故障排查
![[summary of knowledge points] chi square distribution, t distribution, F distribution](/img/a6/bb5cabbfffb0edc9449c4c251354ae.png)
[summary of knowledge points] chi square distribution, t distribution, F distribution

Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village
![阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]](/img/3c/7684b7c594f7871471f89007294703.png)
阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]

B-树系列

【网络安全工具】USB控制软件有什么用
随机推荐
Dongle data collection
PLA not pasted on the bed: 6 simple solutions
UOW of dev XPO comparison
伪装请求头库: anti-useragent
Make: g++: command not found
SystemVerilog learning-10-validation quantification and coverage
[summary of knowledge points] chi square distribution, t distribution, F distribution
SystemVerilog learning-07-class inheritance and package use
HCM Beginner (II) - information type
HCM Beginner (I) - Introduction
JMM详解
Ant new village is one of the special agricultural products that make Tiantou village in Guankou Town, Xiamen become Tiantou village
Distributed lock implementation
异常检测方法梳理,看这篇就够了!
端口扫描工具是什么?端口扫描工具有什么用
图片服务器项目测试
Stack Title: parsing Boolean expressions
json模块
Recueillir des trésors dans le palais souterrain (recherche de mémoire profonde)
XAF Bo of dev XPO comparison