-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparsers.py
178 lines (153 loc) · 7.71 KB
/
parsers.py
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
from pdfminer.high_level import extract_pages
from pdfminer.layout import LAParams
import re
import streamlit as st
def get_page_types(file):
for page in extract_pages(file):
type = None
if any('+Custos BM&F' in g.get_text() for g in page.groups):
type = 'BMF'
else:
type ='B3'
yield type
@st.experimental_memo(show_spinner=False)
def parse_bmf_page(file, page_numbers, password=''):
parsed = []
if not page_numbers:
return parsed
#extrai os ativos
params = LAParams(line_overlap=0, char_margin=10000, line_margin=0.5, word_margin=0.1, boxes_flow=0.5, detect_vertical=False, all_texts=False)
for n, page in enumerate(extract_pages(file, password, page_numbers=page_numbers, laparams=params)):
try:
parsed_page = dict(arquivo=file.name, pagina=page_numbers[n], tipo='BMF')
text = []
for element in page:
if hasattr(element, 'get_text'):
text.append(element.get_text())
text = ''.join(text)
parsed_page['nota']=re.findall(r"Nr. nota((?:\d+)\.*(?:\d*))",text)[0]
parsed_page['data_pregao'] = re.findall(r"Data pregão(\d\d/\d\d/\d\d\d\d)",text)[0]
parsed_page['folha'] = re.findall(r"Folha\s+(\d+)",text)[0]
rex = re.compile(r"(?:C/V.*?Operacional)(.*?)(?:.C\.N\.)", re.IGNORECASE | re.DOTALL)
text = re.search(rex, text)
text = [l for l in text.expand(r"\1").split('\n') if l.strip() != '']
rex_ativo = re.compile(r"""
(?P<CV>C|V)\s+
(?P<Mercadoria>.*?)\s+.?
(?P<Vencimento>\d{2}/\d{2}/\d{4})\s+
(?P<Quantidade>\d+)\s+
(?P<Preço_Ajuste>(?:\d+\.)*(?:\d+,)\d+)\s+
(?P<Tipo_Negocio>.*?)\s+
(?P<Valor_Operacao>(?:\d+\.)*(?:\d+,)\d+)\s+
(?P<DC>D|C)\s+
(?P<Taxa_Operacional>(?:\d+\.)*(?:\d+,)\d+)
""", re.VERBOSE | re.IGNORECASE)
parsed_page['operacoes'] = []
for t in text:
parsed_page['operacoes'].append(re.search(rex_ativo, t).groupdict())
parsed.append(parsed_page)
except Exception as ex:
parsed_page = dict(page_number=page_numbers[n], type='Error')
parsed_page['Message'] = "Erro extraindo operações B3"
parsed_page['Text'] = text
parsed_page['Exception'] = ex
parsed.append(parsed_page)
raise ex
#extrais o resumo
params = LAParams(line_margin=0, word_margin=0.5, char_margin=1000)
for i, page in enumerate(extract_pages(file, password, page_numbers=page_numbers, laparams=params)):
try:
text = []
for element in page:
if hasattr(element, 'get_text'):
text.append(element.get_text())
text = ''.join(text)
custos = re.findall(r"((?:\d+\.)*(?:\d+,)\d\d)",text.split('Venda disponível')[-1]) #todos os valores de "venda disponível em diante"
cols = ["venda_disponivel", "compra_disponivel", "venda_opcao", "compra_opcao", "valor_dos_negocios", "irrf",
"irrf_day_trade", "taxa_operacional", "taxa_registro_bmf", "taxa_bmf", "outos_custos", "iss", "ajuste_de_posicao", "ajuste_day_trade",
"total_custos_operacionais", "outos", "irrf_operacional", "total_conta_investimento", "total_conta_normal", "total_liquido", "total_liquido_da_nota"]
data_bmf = dict(zip(cols, custos))
parsed[i]['resumo'] = data_bmf
except Exception as ex:
parsed_page = dict(page_number=page_numbers[n], type='Error')
parsed_page['Message'] = "Erro extraindo operações B3"
parsed_page['Text'] = text
parsed_page['Exception'] = ex
parsed.append(parsed_page)
raise
return parsed
@st.experimental_memo(show_spinner=False)
def parse_b3_page(file, page_numbers, password=''):
parsed = []
if not page_numbers:
return parsed
#extrai os ativos
params = LAParams(line_overlap=0, char_margin=10000, line_margin=0.5, word_margin=0.1, boxes_flow=0.5, detect_vertical=False, all_texts=False)
for n,page in enumerate(extract_pages(file, password, page_numbers=page_numbers, laparams=params)):
try:
parsed_page = dict(arquivo=file.name, pagina=page_numbers[n], tipo='B3')
text = []
for element in page:
if hasattr(element, 'get_text'):
text.append(element.get_text())
text = ''.join(text)
parsed_page['nota']=re.findall(r"Nr. nota((?:\d+)\.*(?:\d*))",text)[0]
parsed_page['data_pregao'] = re.findall(r"Data pregão(\d\d/\d\d/\d\d\d\d)",text)[0]
parsed_page['folha'] = re.findall(r"Folha\s+(\d+)",text)[0]
rex = re.compile(r"(?:Q\s+Negocia.*?D/C)(.*?)(?:Resumo dos Ne)", re.IGNORECASE | re.DOTALL)
text = re.search(rex, text)
text = [l for l in text.expand(r"\1").split('\n') if l.strip() != '']
rex_ativo = re.compile(r"""
(?P<Negociacao>(.*?))(?=\sC|\sV)\s+
(?P<CV>(C|V))\s+
(?P<titulo>(.*)(?=\s\d+\.?\d*\s))\s+
(?P<quantidade>\d+\.?\d*)\s+
(?P<preco>(?:\d+\.)*(?:\d+,)\d+)\s+
(?P<valor_operacao>(?:\d+\.)*(?:\d+,)\d+)\s+
(?P<DC>D|C)
""", re.VERBOSE | re.IGNORECASE)
parsed_page['operacoes'] = []
for t in text:
op_line = {}
extraction = re.search(rex_ativo, t).groupdict()
op_line.update(extraction)
parsed_page['operacoes'].append(op_line)
parsed.append(parsed_page)
except Exception as ex:
parsed_page = dict(page_number=page_numbers[n], type='Error')
parsed_page['Message'] = "Erro extraindo operações B3"
parsed_page['Text'] = text
parsed_page['Exception'] = ex
parsed.append(parsed_page)
params = LAParams(line_margin=0, word_margin=0, char_margin=10000, boxes_flow=False)
for i, page in enumerate(extract_pages(file, password, page_numbers=page_numbers, laparams=params)):
try:
text = []
for element in page:
if hasattr(element, 'get_text'):
text.append(element.get_text())
text = ''.join(text)
texto_resumo = text.split('Resumo Financeiro')[-1]
rex = re.compile(r"""
((?:\d+\.)*(?:\d+,)\d+(?:\D)(?:\D|\s)+?(?=\d))
""", re.VERBOSE)
parsed[i]['resumo'] = {}
#resumo dos negócios
rex = re.compile(r"""
((?:\d+\.)*(?:\d+,)\d+)(?=.*Bolsa)
""", re.VERBOSE | re.DOTALL)
resumo_dos_negocios = ['Debêntures', 'Vendas a Vista', 'Compras a Vista', 'Opções - Compras', 'Opções - Vendas', 'Operações à termo', 'Valor das Oper. c/ Títulos Públicos (v.nom)', 'Valor das Operações']
resumo_dos_negocios = dict(zip(resumo_dos_negocios, re.findall(rex, texto_resumo)))
parsed[i]['resumo'].update(resumo_dos_negocios)
#Resumo Financeiro
for item in re.findall(rex, texto_resumo):
z = re.findall(r"((?:\d+\.)*(?:\d+,)\d+)(.*)(?:C|D)", item)
z = {i[1]:i[0] for i in z if i}
parsed[i]['resumo'].update(z)
except Exception as ex:
parsed_page = dict(pagina=page_numbers[n], tipo='Error')
parsed_page['Message'] = "Erro extraindo operações B3"
parsed_page['Text'] = text
parsed_page['Exception'] = ex
parsed.append(parsed_page)
return parsed