-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
269 lines (202 loc) · 8.53 KB
/
parser.py
File metadata and controls
269 lines (202 loc) · 8.53 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import openpyxl
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter, column_index_from_string
from collections import namedtuple
import re
Fieldstuple = namedtuple('Fields', 'varieties costumers numbers pieces \
totals prices amounts codes \
\
codes_singleUse numbers_singleUse rates_singleUse')
def find_main_data(book, sheet:str):
page = book.get_sheet_by_name(sheet)
data_beginning_row = None
data_range = 0
for cell in page['A']:
if cell.value == 'date':
data_beginning_row = cell.row + 1
if isinstance(page[f'A{data_beginning_row}'].value, float): # check of row follows next to data_beginng row
continue
else:
raise ValueError("Error: 'date'-row is found but float-type date-cell don't follow further")
if data_beginning_row is not None and type(cell.value) == float:
data_range += 1
elif data_beginning_row is not None and data_range > 1:
break
if data_beginning_row is None:
print(f'no main data finded on "{sheet}"')
return
return data_beginning_row, data_beginning_row + data_range - 1
def find_quantity_columns(book, sheet:str, mainData_range):
page = book.get_sheet_by_name(sheet)
data_beginning_row = str(mainData_range[0])
beginning = None
end = None
for cell in page[data_beginning_row]:
if not isinstance(cell.value, int):
if beginning is not None:
if not isinstance(cell.value, str):
beginning = None
continue
elif ',' in cell.value: #TODO: REG digit-digit-comma-digit-digit
end = cell.column + 1 # +1 is for include 'code' column, that follows quite after amount ( ',' cell.value)
break
continue
elif beginning is None:
beginning = cell.column
if not end - beginning == 5:
raise ValueError("Error during defining range of columns containing quantity values")
return tuple(get_column_letter(i) for i in range(beginning, end+1))
def find_singleUse_setion(book, sheet:str):
page = book.get_sheet_by_name(sheet)
data_beginning_row = None
data_range = 0
c = 0
for cell in page['A']:
if cell.value == 'Single use packaging':
if not page[f'A{cell.row+1}'].value == 'Date':
raise ValueError('error during searching singleUse_range: \
"Date" row don`t follow after "Single use packaging" row')
data_beginning_row = cell.row + 2
continue
if data_beginning_row is not None:
if is_longFormat_date(cell.value):
data_range += 1
continue
elif cell.value == 'Total':
break
if data_beginning_row is None:
print(f'no "Single use packaging" section finded on "{sheet}" in {book}')
return
return data_beginning_row, data_beginning_row + data_range - 1
def find_quantities_singleUse(book, sheet:str, singleUse_range):
page = book.get_sheet_by_name(sheet)
data_beginning_row = singleUse_range[0]
headline_row = str(data_beginning_row - 1)
quantity_columns = [cell.column for cell in page[headline_row]
if cell.value in ('Number', 'Rate')]
return tuple(get_column_letter(i) for i in quantity_columns)
def get_range_from_column(book, sheet:str, col_range:tuple, column_name:str):
"""takes tuple containing (bigining-, end-) column numbers
from find-functions and returns values of cells in this range
"""
page = book.get_sheet_by_name(sheet)
return [i[0].value for i in
page[f'{column_name}{col_range[0]}':f'{column_name}{col_range[1]}']]
def is_varieties_or_costumers(data_list:list):
for i in data_list:
if isinstance(i, str) and i[0].isdigit() and ' ' in i:
continue
else:
return False
return True
def is_longFormat_date(date:str):
result = re.match(r'\d{2}.\d{2}.\d{4}', date)
return not isinstance(result, type(None))
def is_rows_merged(rowData:list):
cell = str(rowData[0])
if '\n' in cell:
return True
return False
def check_codes(codes:list, book, sheet):
for string in codes:
string = str(string)
if not len(string) == 3:
raise ValueError(f'code value {sheet} in {book} on {sheet} does not look like code')
for i in string:
if not i.isdigit():
raise ValueError(f'code value {sheet} in {book} on {sheet} does not look like code')
def check_numbers(numbers:list, book, sheet, column_name):
row_numb=0
for numb in numbers:
row_numb+=1
numb = str(numb)
for i in numb:
if not i.isdigit() and i != '.':
raise ValueError(f'Error during checking Number "{numb}" value (row {row_numb}) in {book} on {sheet}')
def check_rate(rates:list, book, sheet):
"""cheks is format of string like 'dd.mm.yyyy' or not"""
for rate in rates:
rate = str(rate)
res = re.match(r'\d{1},\d{2}', rate)
if isinstance(res, type(None)):
raise ValueError(f'Error during checking Rate value "{rate}" in {book} on {sheet}')
def correct_priece_format(price:str, book, sheet:str):
if not isinstance(price, int):
raise ValueError(f'ValueError while convert to right format value {price}\
from column Prise in book {book}, page {sheet}')
price = str(price)
if len(price) == 3:
return float(f'0.{price}')
elif len(price) > 3:
return float(f'{price[:-3]}.{price[-3:]}')
else:
raise ValueError(f'ValueError: while convert to right format value {price}\
from column Prise in book {book}, page {sheet}')
def adopt_float_format(rate_value):
"""make rate record suitable to convert to float
and convert them via float(rate)"""
result = re.sub(',', '.', rate_value)
return float(result)
def parse(file):
book = load_workbook(file)
sheets = book.get_sheet_names()[1:] #list of sheets without title-page
varieties = []; costumers = []; numbers = []; pieces = []
totals = [] ; prices = [] ; amounts = []; codes = []
codes_singleUse = []; numbers_singleUse = []; rates_singleUse = []
for sh in sheets:
mainData_range = find_main_data(book, sh)
if mainData_range is not None:
variety = get_range_from_column(book, sh, mainData_range, 'C')
if not is_varieties_or_costumers(variety):
raise ValueError(f"Error in column 'variety' in {book} on page '{sheet}'")
varieties += variety
costumer = get_range_from_column(book, sh, mainData_range, 'F')
if not is_varieties_or_costumers(costumer):
raise ValueError(f"Error in column 'costumer' in {book} on page '{sheet}'")
costumers += costumer
quantity_colums = find_quantity_columns(book, sh, mainData_range)
number = get_range_from_column(book, sh, mainData_range, quantity_colums[0])
check_numbers(number, book, sh, 'number')
numbers += number
piece = get_range_from_column(book, sh, mainData_range, quantity_colums[1])
check_numbers(piece, book, sh, 'piece')
pieces += piece
total = get_range_from_column(book, sh, mainData_range, quantity_colums[2])
check_numbers(total, book, sh, 'total')
totals += total
price = get_range_from_column(book, sh, mainData_range, quantity_colums[3])
price = [correct_priece_format(i, book, sh) for i in price]
prices += price
amount = get_range_from_column(book, sh, mainData_range, quantity_colums[4])
amount = [adopt_float_format(i) for i in amount]
amounts += amount
code = get_range_from_column(book, sh, mainData_range, quantity_colums[5])
codes += code
singleUse_range = find_singleUse_setion(book, sh)
if singleUse_range is not None:
code_singleUse = get_range_from_column(book, sh, singleUse_range, 'B')
if len(code_singleUse) == 1 and is_rows_merged(code_singleUse):
code_singleUse = [int(i) for i in code_singleUse[0].split('\n')]
check_codes(code_singleUse, book, sh)
codes_singleUse += code_singleUse
quantities_singleUse = find_quantities_singleUse(book, sh, singleUse_range)
number_singleUse = get_range_from_column(book, sh, singleUse_range, quantities_singleUse[0])
if len(number_singleUse) == 1 and is_rows_merged(number_singleUse):
number_singleUse = [int(i) for i in number_singleUse[0].split('\n')]
check_numbers(number_singleUse, book, sh, 'number_singleUse')
numbers_singleUse += number_singleUse
rate_singleUse = get_range_from_column(book, sh, singleUse_range, quantities_singleUse[1])
if len(rate_singleUse) == 1 and is_rows_merged(rate_singleUse):
rate_singleUse = [i for i in rate_singleUse[0].split('\n')]
check_rate(rate_singleUse, book, sh)
rate_singleUse = [adopt_float_format(i) for i in rate_singleUse]
rates_singleUse += rate_singleUse
retrieved_data = Fieldstuple(varieties, costumers, numbers, pieces,
totals, prices, amounts, codes,
codes_singleUse, numbers_singleUse, rates_singleUse)
return retrieved_data
if __name__ == '__main__':
file = 'pdfFile47.xlsx'
dt = parse(file)
for i in dt:
print(i)