-
Notifications
You must be signed in to change notification settings - Fork 0
/
pivot-table.py
104 lines (78 loc) · 2.86 KB
/
pivot-table.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
import pandas as pd
import numpy as np
import os
import sys
if len(sys.argv) > 1:
bool_receivable = True
cwd = os.path.abspath('')
files = os.listdir(cwd)
file_name = ''
for f in files:
if f.endswith('.xlsx'):
excel_file = pd.ExcelFile(f)
file_name = f
try:
df1 = pd.read_excel(excel_file,'CR',converters={'Follower Office Code': str})
except ValueError:
df1 = pd.DataFrame()
print("There is no claims receivable sheet for this company.")
try:
df2 = pd.read_excel(excel_file, 'PP',converters={'Follower Office Code':str})
except ValueError:
df2 = pd.DataFrame()
print("There is no premium payable sheet for this company.")
pd.set_option('display.float_format', '{:,.2f}'.format)
if not df1.empty:
CR = pd.pivot_table(df1,index='Follower Office Code',values='Total claim amount',aggfunc=np.sum)#,margins=True)
else:
CR = pd.DataFrame()
if not df2.empty:
PP = pd.pivot_table(df2,index='Follower Office Code',values='Net Premium payable',aggfunc=np.sum)#,margins=True)
else:
PP = pd.DataFrame()
if not PP.empty:
if not CR.empty:
all_pivot = pd.merge(PP,CR,left_index=True,right_index=True,how='outer')
all_pivot.fillna(0,inplace=True)
if len(sys.argv) > 1:
all_pivot['Net Receivable by UIIC'] = all_pivot['Total claim amount'] - all_pivot['Net Premium payable']
all_pivot.style.format({'Net Receivable by UIIC':'{0:,.2f}'})
else:
all_pivot['Net Payable by UIIC'] = all_pivot['Net Premium payable'] - all_pivot['Total claim amount']
all_pivot.style.format({'Net Payable by UIIC':'{0:,.2f}'})
all_pivot.rename({'Total claim amount': 'Claims receivable'},axis=1,inplace=True)
else:
all_pivot = PP
else:
all_pivot = CR
all_pivot.loc['Total']= all_pivot.sum(numeric_only=True,axis=0)
#pd.set_option('display.float_format','{:.2f}'.format)
#all_pivot.style.format({'Net Payable by UIIC':'{0:,.2f}'})
print(all_pivot)
#all_pivot.style.format({'Net Payable by UIIC':'${0:,.0f}'})
writer_1 = pd.ExcelWriter("Summary.xlsx",engine='xlsxwriter')
all_pivot.to_excel(writer_1,sheet_name="Summary")
#writer_1.save()
workbook_1 = writer_1.book
worksheet_1 = writer_1.sheets['Summary']
format_currency = workbook_1.add_format({
"num_format": "##,##,#0"
})
format_bold = workbook_1.add_format({
"num_format": "##,##,#","bold":True
})
format_header = workbook_1.add_format({
'bold':True,
'text_wrap':True,
'valign':'top'
})
worksheet_1.set_column("D:D",12,format_bold)
worksheet_1.set_column("B:C",12,format_currency)
worksheet_1.set_row(-1,12,format_bold)
worksheet_1.set_row(0,None,format_header)
#for col, value in enumerate(all_pivot.columns.values):
# worksheet_1.write(0,col,value,format_header)
writer_1.close()
#writer_1.save()
#file_name_new = file_name+"- pivot.xlsx"
#all_pivot.to_excel(file_name_new)