knify 1.8.48__tar.gz → 1.8.50__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: knify
3
- Version: 1.8.48
3
+ Version: 1.8.50
4
4
  Summary: Development tools for python
5
5
  Home-page: https://github.com/qicongsheng/knify
6
6
  Author: qicongsheng
@@ -11,3 +11,4 @@ Platform: any
11
11
  License-File: LICENSE
12
12
  Requires-Dist: loguru
13
13
  Requires-Dist: urllib3
14
+ Requires-Dist: openpyxl
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding:utf-8 -*-
3
+ # Author: qicongsheng
4
+ from typing import Callable
5
+
6
+ from openpyxl.reader.excel import load_workbook
7
+
8
+
9
+ class Header:
10
+ def __init__(self, index: int, name: str | None, transformer: Callable[[object], object] = None):
11
+ self.index = index
12
+ self.name = name
13
+ self.transformer = transformer
14
+
15
+
16
+ class HeaderBuilder:
17
+ def __init__(self):
18
+ self.default_transformer = None
19
+ self.data = []
20
+
21
+ def set_default_transformer(self, transformer: Callable[[object], object] = None):
22
+ self.default_transformer = transformer
23
+ return self
24
+
25
+ def build(self, name: str | None, transformer: Callable[[object], object] = None):
26
+ target_transformer = transformer if transformer is not None else self.default_transformer
27
+ self.data.append(Header(len(self.data), name, target_transformer))
28
+ return self
29
+
30
+ def to_headers(self):
31
+ return self.data
32
+
33
+
34
+ def read_excel(file_path: str, sheet: str | int | None = 0, headers: list[Header] | None = None, start_row: int = 1):
35
+ results = []
36
+ workbook = load_workbook(filename=file_path)
37
+ sheet_ = workbook[sheet] if isinstance(sheet, str) else workbook[workbook.sheetnames[sheet]]
38
+ headers_ = [cell.value for cell in sheet_[1]]
39
+ for row_idx, row in enumerate(sheet_.rows):
40
+ if row_idx < start_row:
41
+ continue
42
+ result = {}
43
+ for header_idx, header_ in enumerate(headers_):
44
+ # 没有传入headers,使用默认header
45
+ if headers is None or len(headers) == 0:
46
+ result[header_] = row[header_idx].value
47
+ # 传入了headers
48
+ else:
49
+ target_headers = list(filter(lambda h_: h_.index == header_idx, headers))
50
+ header = target_headers[0] if target_headers is not None and len(target_headers) > 0 else None
51
+ if header is None:
52
+ continue
53
+ else:
54
+ col_name = header.name if header.name is not None else header_
55
+ cell_value = row[header_idx].value
56
+ cell_value = cell_value if header.transformer is None else header.transformer(row[header_idx].value)
57
+ result[col_name] = cell_value
58
+ results.append(result)
59
+ return results
@@ -8,7 +8,7 @@ def get_pip_name():
8
8
 
9
9
 
10
10
  def get_version():
11
- return '1.8.48'
11
+ return '1.8.50'
12
12
 
13
13
 
14
14
  def print_version():
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding:utf-8 -*-
3
+ # Author: qicongsheng
4
+
5
+
6
+ def partition(list_obj: list, partition_size):
7
+ return [list_obj[i:i + partition_size] for i in range(0, len(list_obj), partition_size)]
8
+
9
+
10
+ def is_empty(list_obj: list):
11
+ return list_obj is None or len(list_obj) == 0
12
+
13
+
14
+ def is_not_empty(list_obj: list):
15
+ return list_obj is not None and len(list_obj) > 0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: knify
3
- Version: 1.8.48
3
+ Version: 1.8.50
4
4
  Summary: Development tools for python
5
5
  Home-page: https://github.com/qicongsheng/knify
6
6
  Author: qicongsheng
@@ -11,3 +11,4 @@ Platform: any
11
11
  License-File: LICENSE
12
12
  Requires-Dist: loguru
13
13
  Requires-Dist: urllib3
14
+ Requires-Dist: openpyxl
@@ -3,6 +3,7 @@ README.md
3
3
  setup.py
4
4
  knify/__init__.py
5
5
  knify/dateutil.py
6
+ knify/excelutil.py
6
7
  knify/fileutil.py
7
8
  knify/help.py
8
9
  knify/listutil.py
@@ -1,2 +1,3 @@
1
1
  loguru
2
2
  urllib3
3
+ openpyxl
@@ -19,6 +19,7 @@ setup(
19
19
  platforms='any',
20
20
  install_requires=[
21
21
  'loguru',
22
- 'urllib3'
22
+ 'urllib3',
23
+ 'openpyxl'
23
24
  ]
24
25
  )
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env python
2
- # -*- coding:utf-8 -*-
3
- # Author: qicongsheng
4
-
5
-
6
- def partition(list_obj, partition_size):
7
- return [list_obj[i:i + partition_size] for i in range(0, len(list_obj), partition_size)]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes