-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkedi_days_3.py
More file actions
90 lines (88 loc) · 3.28 KB
/
kedi_days_3.py
File metadata and controls
90 lines (88 loc) · 3.28 KB
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
#coding=gbk
"""
save_file_path, check_file_path, sheet_name
install module xlwt
demo:
python kedi_days.py save_file_path check_file_path sheet_name
"""
from xlwt import *
import sys
import logging
if __name__ == '__main__':
if len(sys.argv) != 4:
logging.error("""
Usage: python input_name output_name
demo:
python kedi_days.py save_file_path check_file_path sheet_name
""")
exit(1)
save_file_path = sys.argv[1]
check_file_path = sys.argv[2]
sheet_name = sys.argv[3]
fontA = Font()
fontA.name = u'宋体'
fontA.height = 220
styleA = XFStyle()
styleA.font = fontA
fontB = Font()
fontB.name = u'宋体'
fontB.bold = True
fontB.height = 220
styleB = XFStyle()
styleB.font = fontB
headings = [u'店名', u'编号', u'所属公司', u'日期', u'e卡通号', u'交易金额',u'商户号']
import codecs
from itertools import groupby
from collections import defaultdict
fh = codecs.open(filename=check_file_path, mode='r', encoding='gbk')
data1 = map(lambda x: x[0], [[line.strip().split(',')] for line in fh.readlines()])
fh.close()
c = groupby(sorted(data1, key=lambda s: s[1]), lambda f: (f[2], f[1]))
c_dict, c_p_dict = {}, {}
for k, g in c:
(c_dict[k[1]], c_p_dict[k[1]]) = (list(g), k[0])
p_c_dict = defaultdict(list)
for key in c_p_dict:
p_c_dict[c_p_dict[key]].append(key)
book = Workbook(encoding='cp1251')
sheet = book.add_sheet(sheet_name + u'日明细表')
rowx = 0
for colx, value in enumerate(headings):
sheet.write(rowx, colx, value, styleB)
if len(data1) == 0:
logging.error('check file is empty' + check_file_path)
else:
for p_key in p_c_dict:
p_count = rowx + 2
for c_e in p_c_dict[p_key]:
c_count = rowx+2
for row in c_dict[c_e]:
rowx += 1
for colx, value in enumerate(row):
if colx == 5:
value = float(value)
sheet.write(rowx, colx, value, styleA)
rowx += 1
for colx, value in enumerate(['', c_e+u' 汇总', '', '', '',\
Formula("SUBTOTAL(9,F%d:F%d)" % (c_count, rowx)), '']):
if colx == 5:
sheet.write(rowx, colx, value, styleA)
else:
sheet.write(rowx, colx, value, styleB)
rowx += 1
for colx, value in enumerate(['', '', p_key+u' 汇总', '', '',\
Formula("SUBTOTAL(9,F%d:F%d)" % (p_count, rowx-1)), '']):
if colx == 5:
sheet.write(rowx, colx, value, styleA)
else:
sheet.write(rowx, colx, value, styleB)
rowx += 1
for colx, value in enumerate(['', '', u'总计', '', '',\
Formula("SUBTOTAL(9,F%d:F%d)" % (2, rowx-2)), '']):
if colx == 5:
sheet.write(rowx, colx, value, styleA)
else:
sheet.write(rowx, colx, value, styleB)
logging.info(u'执行完成,请查看:'+save_file_path)
book.add_sheet(u"差异调整表")
book.save(save_file_path)