tpltable 0.3.2__tar.gz → 0.3.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tpltable
3
- Version: 0.3.2
3
+ Version: 0.3.3
4
4
  Summary: define "Excel" "Table|StyleTable" "Row|Col|StyleRow|StyleCol". Can dynamic modify excel data & styles. Support multiple kinds of indexes. (70%)
5
5
  Author-email: 2229066748@qq.com
6
6
  Maintainer: Eagle'sBaby
@@ -5,7 +5,7 @@ from setuptools import find_packages, setup
5
5
 
6
6
  setup(
7
7
  name='tpltable',
8
- version='0.3.2',
8
+ version='0.3.3',
9
9
  description='define "Excel" "Table|StyleTable" "Row|Col|StyleRow|StyleCol". Can dynamic modify excel data & styles. Support multiple kinds of indexes. (70%)',
10
10
  author_email='2229066748@qq.com',
11
11
  maintainer="Eagle'sBaby",
@@ -19,7 +19,8 @@ from openpyxl.styles import Font, Color, Alignment, Border, Side, PatternFill
19
19
  NaN = pd.NA
20
20
  nan = pd.NA
21
21
 
22
- isnan = pd.isna
22
+ def isnan(value):
23
+ return pd.isna(value) or str(value) == 'nan'
23
24
 
24
25
  # NOTE: Colorama Start -------------------------------------------------------------------------->
25
26
  # 初始化colorama
@@ -0,0 +1,12 @@
1
+ from tpltable.basic import *
2
+
3
+
4
+ class UnitCell:
5
+ def __init__(self, value, style):
6
+ self.value = value
7
+ self.style = style
8
+
9
+ def __str__(self):
10
+ return str(self.value)
11
+
12
+
@@ -342,6 +342,7 @@ class ExcelFileIOException(Exception):
342
342
 
343
343
 
344
344
  class Excel(ListkeysDict):
345
+ _DEFAULT_PREFIX = 'Sheet'
345
346
  def __init__(self, fpath: str = None, *, style: bool = True):
346
347
  """
347
348
  *Create an Excel object from an excel file or empty
@@ -77,6 +77,24 @@ COLOR_MAGENTA = '00FF00FF'
77
77
  COLOR_DARKMAGENTA = '00800080'
78
78
  COLOR_LIGHTMAGENTA = '00FFC0CB'
79
79
 
80
+ SIDE_THIN_BLACK = Side(style='thin', color=COLOR_BLACK)
81
+
82
+ BORDER_LTRB = Border(SIDE_THIN_BLACK, SIDE_THIN_BLACK, SIDE_THIN_BLACK, SIDE_THIN_BLACK)
83
+ BORDER_NOLEFT = Border(None, SIDE_THIN_BLACK, SIDE_THIN_BLACK, SIDE_THIN_BLACK)
84
+ BORDER_NORIGHT = Border(SIDE_THIN_BLACK, None, SIDE_THIN_BLACK, SIDE_THIN_BLACK)
85
+ BORDER_NOTOP = Border(SIDE_THIN_BLACK, SIDE_THIN_BLACK, None, SIDE_THIN_BLACK)
86
+ BORDER_NOBOTTOM = Border(SIDE_THIN_BLACK, SIDE_THIN_BLACK, SIDE_THIN_BLACK, None)
87
+ BORDER_LT = Border(SIDE_THIN_BLACK, SIDE_THIN_BLACK, None, None)
88
+ BORDER_RB = Border(None, None, SIDE_THIN_BLACK, SIDE_THIN_BLACK)
89
+ BORDER_LB = Border(SIDE_THIN_BLACK, None, None, SIDE_THIN_BLACK)
90
+ BORDER_RT = Border(None, SIDE_THIN_BLACK, SIDE_THIN_BLACK, None)
91
+ BORDER_LR = Border(SIDE_THIN_BLACK, SIDE_THIN_BLACK, None, None)
92
+ BORDER_TB = Border(None, None, SIDE_THIN_BLACK, SIDE_THIN_BLACK)
93
+ BORDER_L = Border(SIDE_THIN_BLACK, None, None, None)
94
+ BORDER_R = Border(None, SIDE_THIN_BLACK, None, None)
95
+ BORDER_T = Border(None, None, SIDE_THIN_BLACK, None)
96
+ BORDER_B = Border(None, None, None, SIDE_THIN_BLACK)
97
+
80
98
 
81
99
  def _GetCellStyle(ws, cell, style_type: str) -> object:
82
100
  if style_type == TYPE_COLOR:
@@ -241,11 +259,11 @@ class Style:
241
259
  # border_style: Incomplete | None = None,
242
260
  _l, _r, _t, _b, _diag = _bd.left, _bd.right, _bd.top, _bd.bottom, _bd.diagonal
243
261
  return {
244
- 'left': {'style': _l.style, 'color': _l.color, 'border_style': _l.border_style},
245
- 'right': {'style': _r.style, 'color': _r.color, 'border_style': _r.border_style},
246
- 'top': {'style': _t.style, 'color': _t.color, 'border_style': _t.border_style},
247
- 'bottom': {'style': _b.style, 'color': _b.color, 'border_style': _b.border_style},
248
- 'diagonal': {'style': _diag.style, 'color': _diag.color, 'border_style': _diag.border_style},
262
+ 'left': {'style': _l.style, 'color': _l.color, 'border_style': _l.border_style} if _l is not None else None,
263
+ 'right': {'style': _r.style, 'color': _r.color, 'border_style': _r.border_style} if _r is not None else None,
264
+ 'top': {'style': _t.style, 'color': _t.color, 'border_style': _t.border_style} if _t is not None else None,
265
+ 'bottom': {'style': _b.style, 'color': _b.color, 'border_style': _b.border_style} if _b is not None else None,
266
+ 'diagonal': {'style': _diag.style, 'color': _diag.color, 'border_style': _diag.border_style} if _diag is not None else None,
249
267
  'diagonal_direction': _bd.diagonal_direction,
250
268
  'vertical': _bd.vertical, 'horizontal': _bd.horizontal,
251
269
  'diagonalUp': _bd.diagonalUp, 'diagonalDown': _bd.diagonalDown,
@@ -301,6 +319,9 @@ class Style:
301
319
  self[key] = value
302
320
 
303
321
 
322
+
323
+
324
+
304
325
  def _raw_style() -> Style:
305
326
  wb = Workbook()
306
327
  ws = wb.active
@@ -4,7 +4,7 @@ import warnings
4
4
  import numpy as np
5
5
  import pandas as pd
6
6
  from tpltable.core import *
7
- from tpltable.style import Style
7
+ from tpltable.style import Style, TYPE_BORDER
8
8
 
9
9
  _PAT_COORD = re.compile(r"([A-Z]+)(\d+)")
10
10
  class EMPTY_INDEX:
@@ -43,6 +43,34 @@ class _ExcelIndexsLike_1DArray:
43
43
 
44
44
  self._reindex()
45
45
 
46
+ @staticmethod
47
+ def _retype_to_unitcell(_from):
48
+ if isinstance(_from, pd.Series):
49
+ if len(_from) and not isinstance(_from.iloc[0]):
50
+ ...
51
+ self._sr = _from
52
+ if copy:
53
+ self._sr = self._sr.copy()
54
+ elif isinstance(_from, np.ndarray):
55
+ # check shape
56
+ s = _from.shape
57
+ if np.ndim(_from) != 1:
58
+ raise ValueError(f"Only 1D array is supported. But got {np.ndim(_from)}D array.(shape={s})")
59
+ if _from.shape[0] != 0:
60
+ ...
61
+
62
+ elif isinstance(_from, list):
63
+ try:
64
+ arr = np.array(_from)
65
+ s = arr.shape
66
+ if np.ndim(arr) != 1:
67
+ raise ValueError(f"Only 1D array is supported. But got {np.ndim(arr)}D array from list.(shape={s})")
68
+ self._sr = pd.Series(arr)
69
+ except Exception as e:
70
+ raise ValueError(f"Failed to convert list to 1D array. {e}")
71
+ else:
72
+ raise ValueError(f"Unsupported type: {type(_from)}")
73
+
46
74
  def _reindex(self):
47
75
  if self._CLS_DEFAULT_INDEX_TYPE == "s":
48
76
  self._sr.index = [get_column_letter(i + 1) for i in range(self.size)]
@@ -61,6 +89,9 @@ class _ExcelIndexsLike_1DArray:
61
89
 
62
90
  return txt
63
91
 
92
+ def __len__(self):
93
+ return self.size
94
+
64
95
  @property
65
96
  def size(self):
66
97
  return self._sr.shape[0]
@@ -776,6 +807,13 @@ class _ExcelIndexsLike_2DArray:
776
807
  sr, sc = self._df.shape
777
808
  sr, sc = sr - 1, sc - 1
778
809
 
810
+ if isinstance(ir, slice):
811
+ ir = ir.stop if ir.stop is not None else sr
812
+
813
+ if isinstance(ic, slice):
814
+ ic = ic.stop if ic.stop is not None else sc
815
+
816
+
779
817
  if ir is None:
780
818
  ir = sr
781
819
  if ic is None:
@@ -787,9 +825,9 @@ class _ExcelIndexsLike_2DArray:
787
825
 
788
826
  # enlarge the table
789
827
  _2d_list = self._df.values.tolist()
790
- if ir >= sr:
828
+ if ir > sr:
791
829
  _2d_list += [[fill] * sc for _ in range(ir - sr)]
792
- if ic >= sc:
830
+ if ic > sc:
793
831
  for _l in _2d_list:
794
832
  _l.extend([fill] * (ic - sc))
795
833
  self._reconstruct(_2d_list, *self._other_raws, copy=False)
@@ -842,7 +880,7 @@ class _ExcelIndexsLike_2DArray:
842
880
  _i0 = slice(None)
843
881
  elif _is_i1_slice and _i1.start is None and _i1.stop is None:
844
882
  _i1 = None
845
- else: # 单个元素赋值
883
+ else: # 元素赋值
846
884
  self._enlarge(_i0, _i1, fill=np.nan)
847
885
  self._df.iloc[_i0, _i1] = value
848
886
 
@@ -957,6 +995,7 @@ class _ExcelIndexsLike_2DArray:
957
995
  row.extend(values)
958
996
  self._reconstruct(_construct, *self._other_raws, copy=False)
959
997
 
998
+
960
999
  def clear(self):
961
1000
  """
962
1001
  Clear the table
@@ -1002,9 +1041,9 @@ class _ExcelIndexsLike_2DArray:
1002
1041
 
1003
1042
  # 先考虑扩张
1004
1043
  if _r < _new_r:
1005
- self.append([[fill.copy() if _has_fill_copy else fill] * _c for _ in range(_new_r - _r)], axis=0)
1044
+ self.append(*[[fill.copy() if _has_fill_copy else fill] * _c for _ in range(_new_r - _r)], axis=0)
1006
1045
  if _c < _new_c:
1007
- self.append([fill.copy() if _has_fill_copy else fill for _ in range(_new_c - _c)], axis=1)
1046
+ self.append(*[fill.copy() if _has_fill_copy else fill for _ in range(_new_c - _c)], axis=1)
1008
1047
 
1009
1048
  # 再考虑缩小 NOTE: 缩小不必考虑label的问题
1010
1049
  if _r > _new_r:
@@ -1033,6 +1072,9 @@ class _ExcelIndexsLike_2DArray:
1033
1072
  return not self._df.equals(other._df)
1034
1073
  return np.array(self._df) != other
1035
1074
 
1075
+ def __len__(self):
1076
+ return self.shape[0]
1077
+
1036
1078
 
1037
1079
 
1038
1080
  class StyleRow(Row):
@@ -1075,7 +1117,7 @@ class Table(_ExcelIndexsLike_2DArray):
1075
1117
  # def __init__(self, _from=None, *, copy: bool = True):
1076
1118
  def __init__(self, data=None, styles=None, merges:list=None, *, copy: bool = True):
1077
1119
  super().__init__(data, copy=copy)
1078
- self._styles = StyleTable() if styles is None else StyleTable(styles)
1120
+ self._styles = StyleTable() if styles is None else (styles.copy() if isinstance(styles, StyleTable) else StyleTable(styles))
1079
1121
  self._merges = []
1080
1122
 
1081
1123
  self.__raw_merges = merges
@@ -1101,7 +1143,7 @@ class Table(_ExcelIndexsLike_2DArray):
1101
1143
  def merges(self):
1102
1144
  return self._merges
1103
1145
 
1104
- def merge(self, key:U[int, str, slice, None], key1:U[int, str, slice, None]=EMPTY_INDEX):
1146
+ def merge(self, key:U[int, str, slice, None], key1:U[int, str, slice, None]=EMPTY_INDEX, *, border:Border=None):
1105
1147
  if key1 is not EMPTY_INDEX:
1106
1148
  key = key, key1
1107
1149
 
@@ -1139,6 +1181,9 @@ class Table(_ExcelIndexsLike_2DArray):
1139
1181
 
1140
1182
  self._merges.append((lt, rb))
1141
1183
 
1184
+ if border is not None:
1185
+ self._styles[key].set(TYPE_BORDER, border)
1186
+
1142
1187
  def _reconstruct(self, *args, **kwargs):
1143
1188
  """
1144
1189
  用于缩小或扩大表格时自动同步调整styles
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tpltable
3
- Version: 0.3.2
3
+ Version: 0.3.3
4
4
  Summary: define "Excel" "Table|StyleTable" "Row|Col|StyleRow|StyleCol". Can dynamic modify excel data & styles. Support multiple kinds of indexes. (70%)
5
5
  Author-email: 2229066748@qq.com
6
6
  Maintainer: Eagle'sBaby
@@ -1 +0,0 @@
1
- from tpltable.basic import *
File without changes
File without changes
File without changes