-
Notifications
You must be signed in to change notification settings - Fork 3
/
pdf_report.py
224 lines (179 loc) · 8.1 KB
/
pdf_report.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import os
from fpdf import FPDF
import matplotlib.pyplot as plt
import primary_info as pi
from PIL import Image
def line_plot(dict_list, parameter, title, comp_list=['AMBEV S.A.'], save_folder='figures'):
# Color list
color_list = ['#45B8AC', '#5B5EA6', '#E15D44', '#B565A7', '#EFC050', '#88B04B', '#6B5B95', '#363945', '#E0B589'
'#FA7A35']
# Create figure
fig = plt.figure(figsize=(12, 5))
fig.gca().spines["right"].set_visible(False)
fig.gca().spines["top"].set_visible(False)
# Add plots for each company
for comp_index in range(len(dict_list)):
color = color_list[comp_index]
list_year = dict_list[comp_index]['year_columns']
#print(list_year)
#print(f'{parameter} e :{dict_list[comp_index][parameter]}')
plt.plot(list_year, dict_list[comp_index][parameter], label=f'{comp_list[comp_index]}', linewidth=1.4, color=color)
# Aditional seetings
plt.title(title)
plt.legend(bbox_to_anchor=(1, 0.5), loc='upper left', frameon=False)
plt.xlabel('Anos')
plt.ylabel(title)
# Margem Bruta - seetings
if parameter == 'marg_brut_list':
plt.axhline(y=22, color='#D22B2B', linestyle='--', linewidth=0.9)
plt.axhline(y=40, color='#7FFF00', linestyle='--', linewidth=0.9)
# Margem Líquida - seetings
if parameter == 'marg_liq_list':
plt.axhline(y=10, color='#D22B2B', linestyle='--', linewidth=0.9)
plt.axhline(y=20, color='#7FFF00', linestyle='--', linewidth=0.9)
# Despesas VGA - seetings
if parameter == 'vga_lucro_brut_list':
plt.axhline(y=-80, color='#D22B2B', linestyle='--', linewidth=0.9)
# Despesas juros - seetings
if parameter == 'juros_lucro_oper_list':
plt.axhline(y=-15, color='#7FFF00', linestyle='--', linewidth=0.9)
# Coeficiente de liquidez - seetings
if parameter == 'coef_liquidez':
plt.axhline(y=1, color='#D22B2B', linestyle='--', linewidth=0.9)
# Despesas com ativos fixos sobre o lucro líquido do exercício - seetings
if parameter == 'desp_ativo_fixo_lucro_liq_exerc_list':
plt.axhline(y=-50, color='#7FFF00', linestyle='--', linewidth=0.9)
plt.axhline(y=-25, color='#7FFF00', linestyle='--', linewidth=0.9)
if parameter == 'divida_tot_lucro_oper_list':
plt.axhline(y=4, color='#D22B2B', linestyle='--', linewidth=0.9)
# Save the figure in "figures" folder
plt.savefig(f'{save_folder}\\{parameter}.png', bbox_inches='tight', transparent=True)
def image_setter(image, folder_fig, pdf_object):
pdf_w = 210
pdf_h = 297
image_handle = Image.open(f'{folder_fig}\\{image}')
im_size = image_handle.size # Return (width, heigth)
# Add page if imagem is bigger than left space + 20 of margin
if (im_size[1]/im_size[0]) * 180 >= (pdf_h - pdf_object.get_y() - 20):
pdf_object.add_page()
pdf_object.lines()
pdf_object.image(f'{folder_fig}\\{image}', x=(pdf_w/2 - 180/2), w=180)
pdf_object.ln(10)
# Input companies for analysis
company_list = ['INSTITUTO HERMES PARDINI S.A.']
# Collect data from function worked_info
return_dict_list = pi.worked_info(companies=company_list)
print('-+-' * 20)
print('CRIANDO IMAGENS ...')
# Create list of years
year_list = []
dir_elements = os.listdir('raw_dfp')
for element in dir_elements:
if '20' in element and element == '2017':
year_list.append(2016)
year_list.append(2017)
elif '20' in element:
year_list.append(int(element))
# Create figure folder
fig_folder = 'figures'
directory_elements = os.listdir()
if fig_folder not in directory_elements:
os.mkdir(f'{fig_folder}')
elif os.listdir(f'{fig_folder}') != []:
fig_folder_elements = os.listdir(f'{fig_folder}')
for element in fig_folder_elements:
os.remove(f'{fig_folder}\\{element}')
# Create PDF class
class PDF(FPDF):
def lines(self):
self.rect(2.5, 2.5, 205.0, 292.0)
# Create .pdf object
pdf = PDF('P', 'mm', 'A4')
# Set A4 page total dimensions
pdf_w = 210
pdf_h = 297
pdf.set_auto_page_break(True, margin=20)
pdf.add_page()
pdf.lines()
pdf.set_font('Courier', '', 20)
pdf.set_x(x=55)
pdf.cell(100, 10, 'Análise de Demonstração Financeira', align='C')
pdf.ln(30)
# Plot Linha
line_plot(return_dict_list, 'marg_brut_list', 'Margem Bruta (%)', comp_list=company_list)
line_plot(return_dict_list, 'marg_liq_list', 'Margem Líquida (%)', comp_list=company_list)
line_plot(return_dict_list, 'vga_lucro_brut_list', 'Despesas VGA sobre o Lucro Bruto (%)', comp_list=company_list)
line_plot(return_dict_list, 'ped_lucro_brut_list', 'Despesas Pesquisa sobre o Lucro Bruto (%)', comp_list=company_list)
line_plot(return_dict_list, 'deprec_lucro_brut_list', 'Depreciciação sobre o Lucro Bruto (%)', comp_list=company_list)
line_plot(return_dict_list, 'juros_lucro_oper_list', 'Juros sobre o Lucro Operacional (%)', comp_list=company_list)
line_plot(return_dict_list, 'lucro_liq_list', 'Lucro líquido (mil reais)', comp_list=company_list)
line_plot(return_dict_list, 'lucro_brut_list', 'Lucro Bruto (mil reais)', comp_list=company_list)
line_plot(return_dict_list, 'lucroporacao_list', 'Lucro por ação (mil reais)', comp_list=company_list)
line_plot(return_dict_list, 'coef_liquidez_list', 'Coeficiente de Liquidez (Ativo Circulante/Passivo Circulante)', comp_list=company_list)
line_plot(return_dict_list, 'imobilizado_list', 'Ativo Imobilizado (mil reais)', comp_list=company_list)
line_plot(return_dict_list, 'passivo_tot_patrliq_list', 'Passivo Total/Patrimônio Líquido', comp_list=company_list)
line_plot(return_dict_list, 'roe_list', 'ROE (%)', comp_list=company_list)
line_plot(return_dict_list, 'roa_list', 'ROA (%)', comp_list=company_list)
line_plot(return_dict_list, 'lucro_acumul_list', 'Lucro Acumulado (mil reais)', comp_list=company_list)
line_plot(return_dict_list, 'desp_ativo_fixo_lucro_liq_exerc_list', 'Despesa com Ativos Fixos/Lucro Líquido (%)', comp_list=company_list)
line_plot(return_dict_list, 'divida_curto_tot_list', 'Dívida de Curto Prazo / Dívida Total (%)', comp_list=company_list)
line_plot(return_dict_list, 'divida_tot_lucro_oper_list', 'Divida Bruta / Lucro Operacional', comp_list=company_list)
print('-+-' * 20)
print('CRIANDO PDF ...')
# Rentability indicators
pdf.set_font('ZapfDingbats', '', 5)
pdf.cell(4, 10, 'l', align='L')
pdf.set_font('Courier', 'I', 15)
pdf.set_x(x=15)
pdf.cell(100, 10, 'Indicadores de Rentabilidade', align='L')
pdf.ln(20)
rent_list = ['marg_brut_list.png', 'marg_liq_list.png', 'lucroporacao_list.png', 'roe_list.png',
'roa_list.png']
for image in rent_list:
image_setter(image, fig_folder, pdf)
# Growth indicators
pdf.ln(20)
pdf.set_font('ZapfDingbats', '', 5)
pdf.cell(4, 10, 'l', align='L')
pdf.set_font('Courier', 'I', 15)
pdf.set_x(x=15)
pdf.cell(100, 10, 'Indicadores de Crescimento', align='L')
pdf.ln(20)
growth_list = ['lucro_brut_list.png', 'lucro_liq_list.png', 'lucro_acumul_list.png']
for image in growth_list:
image_setter(image, fig_folder, pdf)
# Expenditure indicators
pdf.ln(20)
pdf.set_font('ZapfDingbats', '', 5)
pdf.cell(4, 10, 'l', align='L')
pdf.set_font('Courier', 'I', 15)
pdf.set_x(x=15)
pdf.cell(100, 10, 'Indicadores de Despesas', align='L')
pdf.ln(20)
desp_list = ['vga_lucro_brut_list.png', 'ped_lucro_brut_list.png', 'deprec_lucro_brut_list.png',
'juros_lucro_oper_list.png']
for image in desp_list:
image_setter(image, fig_folder, pdf)
# Debt indicators
pdf.ln(20)
pdf.set_font('ZapfDingbats', '', 5)
pdf.cell(4, 10, 'l', align='L')
pdf.set_font('Courier', 'I', 15)
pdf.set_x(x=15)
pdf.cell(100, 10, 'Indicadores de Endividamento', align='L')
pdf.ln(20)
debt_list = ['coef_liquidez_list.png', 'passivo_tot_patrliq_list.png', 'divida_curto_tot_list.png', 'divida_tot_lucro_oper_list.png']
for image in debt_list:
image_setter(image, fig_folder, pdf)
# Other indicators
pdf.ln(20)
pdf.set_font('ZapfDingbats', '', 5)
pdf.cell(4, 10, 'l', align='L')
pdf.set_font('Courier', 'I', 15)
pdf.set_x(x=15)
pdf.cell(100, 10, 'Outros Indicadores', align='L')
pdf.ln(20)
other_list = ['imobilizado_list.png', 'desp_ativo_fixo_lucro_liq_exerc_list.png']
for image in other_list:
image_setter(image, fig_folder, pdf)
pdf.output('DFP Report.pdf', 'F')