-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcel.py
More file actions
156 lines (130 loc) · 4.42 KB
/
excel.py
File metadata and controls
156 lines (130 loc) · 4.42 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
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
#coding=utf8
import sys
import openpyxl
import xlrd
def find_columns(ws, labels, from_row = 1):
for row in xrange(from_row,from_row+MT_ROW_LIMIT):
col_inds = {}
for col in xrange(1,MT_COL_LIMIT):
cell = ws.cell(row = row, column = col)
if cell.value in [None, ""] or cell.data_type != 's':
continue
value = unicode(cell.value).lower()
for l in set(labels) - set(col_inds.keys()):
for text in MT_LABELS[l]:
if text in value:
col_inds[l] = col
if len(col_inds.keys()) == len(labels):
return (row, col_inds)
return False
# возвращает индекс первой строки удовлетворяющей условиям
def find_row(ws, from_row, col_inds, funcs, row_count=1):
prev_row = from_row -1
rcount = 0
for row in xrange(from_row, from_row+MT_ROW_LIMIT2):
ret = True
for l in col_inds:
if not funcs[l](ws.cell(row = row, column = col_inds[l])):
ret = False
continue
if ret:
if rcount == row_count-1:
return row
elif prev_row == (row-1):
rcount += 1
else:
rcount = 0
prev_row = row
return False
def open_xls_as_xlsx(filename):
# first open using xlrd
book = xlrd.open_workbook(filename)
index = 0
nrows, ncols = 0, 0
while nrows * ncols == 0:
sheet = book.sheet_by_index(index)
nrows = sheet.nrows
ncols = sheet.ncols
index += 1
# prepare a xlsx sheet
book1 = openpyxl.Workbook()
sheet1 = book1.get_active_sheet()
for row in xrange(0, nrows):
for col in xrange(0, ncols):
sheet1.cell(row=row+1, column=col+1).value = sheet.cell_value(row, col)
return book1
# возвращает список товаров
def get_products(filename):
# открываем файл
if filename[-4:] == '.xls':
wb = open_xls_as_xlsx(filename)
else:
wb = openpyxl.load_workbook(filename)
ws = wb.active
# получаем номер строки и столбцов с заголовками
lrow_ind, col_inds = find_columns(ws, ["price", "count", "name"])
# получаем номер первой и последней строки с товарам
srow_ind = find_row(ws, lrow_ind+1, col_inds, MT_FUNCS_NOCLEAN)
frow_ind = find_row(ws, srow_ind+1, col_inds, MT_FUNCS_CLEAN)
# формируем данные
products = []
for r in xrange(srow_ind, frow_ind):
products.append({l: ws.cell(row = r, column = col_inds[l]).value for l in col_inds})
print 'products',len(products)
return products
# сохранение списка товаров в файл
def save_products(filename, products):
# открываем файл
wb = openpyxl.load_workbook(filename,guess_types=False,keep_vba=True)
ws = wb.active
# получаем номер строки и столбцов с заголовками
lrow_ind, col_inds = find_columns(ws, ["price", "count", "name", "sum"])
print 'ab',lrow_ind, col_inds
sum_col_ind = col_inds["sum"]
del col_inds["sum"]
# получаем номер последней строки после товаров
row_ind = find_row(ws, lrow_ind+1, col_inds, MT_FUNCS_CLEAN, row_count=2)
print 'sd',row_ind
row_ind -= 1
# записываем данные
first_row_ind = row_ind
for prod in products:
for key in prod:
print 'r', row_ind, 'c', col_inds[key]
ws.cell(row = row_ind, column = col_inds[key]).value = prod[key]
row_ind += 1
# подбиваем сумму
letter = openpyxl.cell.get_column_letter(sum_col_ind)
cell = ws.cell(row = row_ind, column = sum_col_ind)
cell.value = "=SUBTOTAL(109,%s%s:%s%s)" % (letter, first_row_ind, letter, row_ind-1)
# для защищенных полей обязательно почистить атрибуты
ws.formula_attributes[cell.coordinate] = {}
# сохраняем в файл
wb.save(filename+'_new.xlsx')
def is_zero(cell):
return cell.value in ["", None, 0, "0"]
def is_digit(cell):
return cell.data_type == 'n'
def is_string(cell):
return cell.data_type == 's' and cell.value != ""
# область поиска заголовков
MT_COL_LIMIT = 100
MT_ROW_LIMIT = 100
# область поиска строк
MT_ROW_LIMIT2 = 50000
MT_FUNCS_CLEAN = {
"name": is_zero,
"count": is_zero,
"price": is_zero,
}
MT_FUNCS_NOCLEAN = {
"name": is_string,
"count": is_digit,
"price": is_digit,
}
MT_LABELS = {
"name": [u"товар", u"название", u"наименование"],
"count": [u"количество", u"кол-во", u"колво", u"кол"],
"price": [u"цена"],
"sum": [u"сумма", u"сум"],
}