forked from raizen-analytics/data-engineering-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache_extractor.py
More file actions
34 lines (29 loc) · 908 Bytes
/
Copy pathcache_extractor.py
File metadata and controls
34 lines (29 loc) · 908 Bytes
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
from openpyxl import load_workbook
import pandas as pd
planilha = load_workbook('vendas-combustiveis-m3.xlsx')
sheet = planilha['Plan1']
#Extrai dados do cache das tabelas dinâmicas e cria um dataframe
def create_df_cache(cache):
total = []
cols = {}
for i in cache.cacheFields:
cols[i.name] = i.name
for linhas in cache.records.r:
linha = []
for cll in linhas._fields:
try:
linha.append(cll.v)
except AttributeError:
linha.append(None)
total.append(linha)
df = pd.DataFrame(columns=cols, data=total)
return df
#Verificar index das tabelas (Apenas necessário uma vez)
'''
for table in sheet._pivots:
print(table.name)
'''
cx_pivot_oil = sheet._pivots[3].cache
cx_pivot_diesel = sheet._pivots[1].cache
df_diesel = create_df_cache(cx_pivot_diesel)
df_oil = create_df_cache(cx_pivot_oil)