knify 1.8.54__tar.gz → 1.8.56__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {knify-1.8.54 → knify-1.8.56}/PKG-INFO +1 -1
- {knify-1.8.54 → knify-1.8.56}/knify/dateutil.py +7 -1
- {knify-1.8.54 → knify-1.8.56}/knify/excelutil.py +22 -14
- {knify-1.8.54 → knify-1.8.56}/knify/help.py +1 -1
- {knify-1.8.54 → knify-1.8.56}/knify/listutil.py +4 -0
- {knify-1.8.54 → knify-1.8.56}/knify/objutil.py +4 -0
- {knify-1.8.54 → knify-1.8.56}/knify.egg-info/PKG-INFO +1 -1
- {knify-1.8.54 → knify-1.8.56}/LICENSE +0 -0
- {knify-1.8.54 → knify-1.8.56}/README.md +0 -0
- {knify-1.8.54 → knify-1.8.56}/knify/__init__.py +0 -0
- {knify-1.8.54 → knify-1.8.56}/knify/fileutil.py +0 -0
- {knify-1.8.54 → knify-1.8.56}/knify/logger.py +0 -0
- {knify-1.8.54 → knify-1.8.56}/knify/threadutil.py +0 -0
- {knify-1.8.54 → knify-1.8.56}/knify/warnutil.py +0 -0
- {knify-1.8.54 → knify-1.8.56}/knify.egg-info/SOURCES.txt +0 -0
- {knify-1.8.54 → knify-1.8.56}/knify.egg-info/dependency_links.txt +0 -0
- {knify-1.8.54 → knify-1.8.56}/knify.egg-info/requires.txt +0 -0
- {knify-1.8.54 → knify-1.8.56}/knify.egg-info/top_level.txt +0 -0
- {knify-1.8.54 → knify-1.8.56}/setup.cfg +0 -0
- {knify-1.8.54 → knify-1.8.56}/setup.py +0 -0
@@ -18,15 +18,21 @@ def now_str(format: str = FORMAT_YYMMDDHMS) -> str:
|
|
18
18
|
return date_to_str(now(), format)
|
19
19
|
|
20
20
|
|
21
|
-
def date_to_str(date_obj: datetime, format: str = FORMAT_YYMMDDHMS)
|
21
|
+
def date_to_str(date_obj: datetime, format: str = FORMAT_YYMMDDHMS):
|
22
|
+
if date_obj is None:
|
23
|
+
return None
|
22
24
|
if type(date_obj) == datetime.timedelta:
|
23
25
|
return (datetime.datetime(1970, 1, 1) + date_obj).strftime(format)
|
24
26
|
return date_obj.strftime(format)
|
25
27
|
|
26
28
|
|
27
29
|
def str_to_date(str_obj: str, format: str) -> datetime:
|
30
|
+
if str_obj is None:
|
31
|
+
return None
|
28
32
|
return datetime.datetime.strptime(str_obj, format)
|
29
33
|
|
30
34
|
|
31
35
|
def date_to_timestamp(date_obj: datetime):
|
36
|
+
if date_obj is None:
|
37
|
+
return None
|
32
38
|
return time.mktime(date_obj.timetuple())
|
@@ -5,6 +5,9 @@ from typing import Callable
|
|
5
5
|
|
6
6
|
from openpyxl.reader.excel import load_workbook
|
7
7
|
|
8
|
+
from . import listutil
|
9
|
+
from . import objutil
|
10
|
+
|
8
11
|
|
9
12
|
class Header:
|
10
13
|
def __init__(self, index: int, name: str | None, transformer: Callable[[object], object] = None):
|
@@ -18,28 +21,28 @@ class HeaderBuilder:
|
|
18
21
|
self.default_transformer = None
|
19
22
|
self.headers = []
|
20
23
|
|
21
|
-
def set_default_transformer(self, transformer: Callable[[object], object] = None):
|
24
|
+
def set_default_transformer(self, transformer: Callable[[object], object] = None) -> object:
|
22
25
|
self.default_transformer = transformer
|
23
26
|
return self
|
24
27
|
|
25
|
-
def set_names(self, names: list[str] = None):
|
28
|
+
def set_names(self, names: list[str] = None) -> object:
|
26
29
|
for index_, name in enumerate(names):
|
27
30
|
self.headers.append(Header(index_, name, self.default_transformer))
|
28
31
|
return self
|
29
32
|
|
30
|
-
def set_transformer(self, name: str, transformer: Callable[[object], object] = None):
|
33
|
+
def set_transformer(self, name: str, transformer: Callable[[object], object] = None) -> object:
|
31
34
|
for header in self.headers:
|
32
35
|
if name == header.name:
|
33
36
|
header.transformer = transformer
|
34
37
|
return self
|
35
38
|
|
36
|
-
def append(self, index: int, name: str | None, transformer: Callable[[object], object] = None):
|
37
|
-
|
38
|
-
target_transformer = transformer
|
39
|
-
self.headers.append(Header(
|
39
|
+
def append(self, index: int, name: str | None, transformer: Callable[[object], object] = None) -> object:
|
40
|
+
target_index = objutil.default_if_none(index, len(self.headers))
|
41
|
+
target_transformer = objutil.default_if_none(transformer, self.default_transformer)
|
42
|
+
self.headers.append(Header(target_index, name, target_transformer))
|
40
43
|
return self
|
41
44
|
|
42
|
-
def to_headers(self):
|
45
|
+
def to_headers(self) -> list[Header]:
|
43
46
|
return self.headers
|
44
47
|
|
45
48
|
|
@@ -47,26 +50,31 @@ def read_excel(file_path: str, sheet: str | int | None = 0, headers: list[Header
|
|
47
50
|
results = []
|
48
51
|
workbook = load_workbook(filename=file_path)
|
49
52
|
sheet_ = workbook[sheet] if isinstance(sheet, str) else workbook[workbook.sheetnames[sheet]]
|
50
|
-
headers_ = [cell.value for cell in sheet_[
|
53
|
+
headers_ = [cell.value for cell in sheet_[start_row]]
|
51
54
|
for row_idx, row in enumerate(sheet_.rows):
|
52
55
|
if row_idx < start_row:
|
53
56
|
continue
|
54
57
|
result = {}
|
55
58
|
for header_idx, header_ in enumerate(headers_):
|
56
59
|
# 没有传入headers,使用默认header
|
57
|
-
if
|
60
|
+
if listutil.is_empty(headers):
|
58
61
|
result[header_] = row[header_idx].value
|
59
62
|
# 传入了headers
|
60
63
|
else:
|
61
|
-
|
62
|
-
header = target_headers[0] if target_headers is not None and len(target_headers) > 0 else None
|
64
|
+
header = listutil.find_first(list(filter(lambda h_: h_.index == header_idx, headers)))
|
63
65
|
if header is None:
|
64
66
|
continue
|
65
67
|
else:
|
66
|
-
col_name =
|
68
|
+
col_name = objutil.default_if_none(header.name, header_)
|
67
69
|
cell_value = row[header_idx].value
|
68
70
|
cell_value = cell_value if header.transformer is None else header.transformer(row[header_idx].value)
|
69
71
|
result[col_name] = cell_value
|
70
|
-
if
|
72
|
+
if objutil.has_keys(result):
|
71
73
|
results.append(result)
|
72
74
|
return results
|
75
|
+
|
76
|
+
|
77
|
+
def read_headers(file_path: str, sheet: str | int | None = 0, header_row: int = 0):
|
78
|
+
workbook = load_workbook(filename=file_path)
|
79
|
+
sheet_ = workbook[sheet] if isinstance(sheet, str) else workbook[workbook.sheetnames[sheet]]
|
80
|
+
return [cell.value for cell in sheet_[header_row + 1]]
|
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
|
File without changes
|
File without changes
|
File without changes
|