当前位置:网站首页>码力十足学量化|如何在财务报告寻找合适的财务公告
码力十足学量化|如何在财务报告寻找合适的财务公告
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文本解析及筛选完毕!")
边栏推荐
猜你喜欢

Picture server project test
![Pit of kotlin bit operation (bytes[i] and 0xff error)](/img/2c/de0608c29d8af558f6f8dab4eb7fd8.png)
Pit of kotlin bit operation (bytes[i] and 0xff error)

Solve the problem of garbled files uploaded by Kirin v10

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

ForkJoin和Stream流测试

Skywalking integrated Nacos dynamic configuration

On siem

High order binary balanced tree

HCM Beginner (II) - information type

让田头村变甜头村的特色农产品是仙景芋还是白菜
随机推荐
10 golang operator
ArcServer密码重置(账号不可以重置)
【KV260】利用XADC生成芯片温度曲线图
Although pycharm is marked with red in the run-time search path, it does not affect the execution of the program
DHT11 temperature and humidity sensor
Small guide for rapid completion of mechanical arm (VI): stepping motor driver
srpingboot security demo
浏览器端保存数据到本地文件
Make: g++: command not found
To sort out the anomaly detection methods, just read this article!
High order binary balanced tree
Factorial divisor (unique decomposition theorem)
JDBC database operation
PLA not pasted on the bed: 6 simple solutions
HCM Beginner (IV) - time
【LeetCode】Day91-存在重复元素
讓田頭村變甜頭村的特色農產品是仙景芋還是白菜
HCM Beginner (III) - quickly enter pa70 and pa71 to browse employee information PA10
Differences between in and exists in MySQL
MySQL怎么存储emoji?