pyfastexcel 0.0.1__py3-none-any.whl
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.
- pyfastexcel/__init__.py +0 -0
- pyfastexcel/driver.py +260 -0
- pyfastexcel/pyfastexcel.dll +0 -0
- pyfastexcel/pyfastexcel.so +0 -0
- pyfastexcel-0.0.1.dist-info/LICENSE +28 -0
- pyfastexcel-0.0.1.dist-info/METADATA +40 -0
- pyfastexcel-0.0.1.dist-info/RECORD +9 -0
- pyfastexcel-0.0.1.dist-info/WHEEL +5 -0
- pyfastexcel-0.0.1.dist-info/top_level.txt +1 -0
pyfastexcel/__init__.py
ADDED
|
File without changes
|
pyfastexcel/driver.py
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
from openpyxl_style_writer import RowWriter, CustomStyle
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import ctypes
|
|
10
|
+
import msgspec
|
|
11
|
+
import base64
|
|
12
|
+
|
|
13
|
+
BASE_DIR = Path(__file__).resolve().parent
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class PyExcelizeDriver(RowWriter):
|
|
17
|
+
BORDER_TO_INDEX = {
|
|
18
|
+
None: 0,
|
|
19
|
+
'thick': 5,
|
|
20
|
+
'slantDashDot': 13,
|
|
21
|
+
'dotted': 4,
|
|
22
|
+
'hair': 7,
|
|
23
|
+
'dashed': 3,
|
|
24
|
+
'double': 6,
|
|
25
|
+
'mediumDashDotDot': 12,
|
|
26
|
+
'medium': 2,
|
|
27
|
+
'dashDotDot': 11,
|
|
28
|
+
'thin': 1,
|
|
29
|
+
'dashDot': 9,
|
|
30
|
+
'mediumDashed': 8,
|
|
31
|
+
'mediumDashDot': 10,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
def __init__(self):
|
|
35
|
+
self.pyexcelize_data = {'Sheet1': self._get_default_sheet()}
|
|
36
|
+
self.sheet = 'Sheet1'
|
|
37
|
+
self.style_name_map = {}
|
|
38
|
+
|
|
39
|
+
def _get_default_sheet(self) -> dict[str, dict[str, list]]:
|
|
40
|
+
return {
|
|
41
|
+
'Header': [],
|
|
42
|
+
'Data': [],
|
|
43
|
+
'MergeCells': [],
|
|
44
|
+
'Width': [],
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
def create_excel(self) -> bytes:
|
|
48
|
+
self._create_style()
|
|
49
|
+
self._create_single_header()
|
|
50
|
+
self._create_body()
|
|
51
|
+
return self._read_lib_and_create_excel()
|
|
52
|
+
|
|
53
|
+
def remove_sheet(self, sheet: str) -> None:
|
|
54
|
+
self.pyexcelize_data.pop(sheet)
|
|
55
|
+
|
|
56
|
+
def create_sheet(self, sheet_name: str) -> None:
|
|
57
|
+
self.pyexcelize_data[sheet_name] = self._get_defualt_sheet()
|
|
58
|
+
|
|
59
|
+
def switch_sheet(self, sheet_name: str) -> None:
|
|
60
|
+
self.sheet = sheet_name
|
|
61
|
+
if self.pyexcelize_data.get(sheet_name) is None:
|
|
62
|
+
self.pyexcelize_data[sheet_name] = self._get_default_sheet()
|
|
63
|
+
|
|
64
|
+
def create_single_header(self) -> None:
|
|
65
|
+
pass
|
|
66
|
+
|
|
67
|
+
def create_body(self) -> None:
|
|
68
|
+
pass
|
|
69
|
+
|
|
70
|
+
def _read_lib(self, lib_path: str) -> str:
|
|
71
|
+
lib_path = f'{BASE_DIR}/pyfastexcel' if lib_path is None else lib_path
|
|
72
|
+
if lib_path.split('.')[-1] not in ('so', 'dll'):
|
|
73
|
+
if sys.platform.startswith('linux'):
|
|
74
|
+
lib_path += '.so'
|
|
75
|
+
elif sys.platform.startswith('win32'):
|
|
76
|
+
lib_path += '.dll'
|
|
77
|
+
lib = ctypes.CDLL(lib_path, winmode=0)
|
|
78
|
+
return lib
|
|
79
|
+
|
|
80
|
+
def _read_lib_and_create_excel(self, lib_path: str = None) -> bytes:
|
|
81
|
+
pyexcelize = self._read_lib(lib_path)
|
|
82
|
+
json_data = msgspec.json.encode(self.pyexcelize_data)
|
|
83
|
+
create_excel = pyexcelize.Export
|
|
84
|
+
free_pointer = pyexcelize.FreeCPointer
|
|
85
|
+
free_pointer.argtypes = [ctypes.c_void_p]
|
|
86
|
+
create_excel.argtypes = [ctypes.c_char_p]
|
|
87
|
+
create_excel.restype = ctypes.c_void_p
|
|
88
|
+
byte_data = create_excel(json_data)
|
|
89
|
+
decoded_bytes = base64.b64decode(ctypes.cast(byte_data, ctypes.c_char_p).value)
|
|
90
|
+
free_pointer(byte_data)
|
|
91
|
+
return decoded_bytes
|
|
92
|
+
|
|
93
|
+
def _create_style(self) -> None:
|
|
94
|
+
style_collections = self._get_style_collections()
|
|
95
|
+
self.style_map_name = {val: key for key, val in style_collections.items()}
|
|
96
|
+
self.style_map = {}
|
|
97
|
+
for key, val in style_collections.items():
|
|
98
|
+
self._update_style_map(key, val)
|
|
99
|
+
self.pyexcelize_data['Style'] = self.style_map
|
|
100
|
+
|
|
101
|
+
def _get_style_collections(self) -> dict[str, CustomStyle]:
|
|
102
|
+
return {
|
|
103
|
+
attr: getattr(self, attr)
|
|
104
|
+
for attr in dir(self)
|
|
105
|
+
if not callable(getattr(self, attr)) and isinstance(getattr(self, attr), CustomStyle)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
def _get_default_style(self) -> dict[str, dict[str, Any] | str]:
|
|
109
|
+
return {
|
|
110
|
+
'Font': {},
|
|
111
|
+
'Fill': {},
|
|
112
|
+
'Border': {},
|
|
113
|
+
'Alignment': {},
|
|
114
|
+
'Protection': {},
|
|
115
|
+
'CustomNumFmt': 'general',
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
def _update_style_map(self, style_name: str, custom_style: CustomStyle) -> None:
|
|
119
|
+
self.style_map[style_name] = self._get_default_style()
|
|
120
|
+
self.style_map[style_name]['Font'] = self._get_font_style(custom_style)
|
|
121
|
+
self.style_map[style_name]['Fill'] = self._get_fill_style(custom_style)
|
|
122
|
+
self.style_map[style_name]['Border'] = self._get_border_style(custom_style)
|
|
123
|
+
self.style_map[style_name]['Alignment'] = self._get_alignment_style(custom_style)
|
|
124
|
+
self.style_map[style_name]['Protection'] = self._get_protection_style(custom_style)
|
|
125
|
+
self.style_map[style_name]['CustomNumFmt'] = custom_style.number_format
|
|
126
|
+
|
|
127
|
+
def _get_font_style(self, style: CustomStyle) -> dict[str, str | int | bool | None]:
|
|
128
|
+
font_style_map = {}
|
|
129
|
+
if style.font.name:
|
|
130
|
+
font_style_map['Name'] = style.font.name
|
|
131
|
+
if style.font.sz:
|
|
132
|
+
font_style_map['Size'] = style.font.sz
|
|
133
|
+
if style.font.family:
|
|
134
|
+
font_style_map['Family'] = style.font.family
|
|
135
|
+
if style.font.b:
|
|
136
|
+
font_style_map['Bold'] = style.font.b
|
|
137
|
+
if style.font.i:
|
|
138
|
+
font_style_map['Italic'] = style.font.i
|
|
139
|
+
if style.font.strike:
|
|
140
|
+
font_style_map['Strike'] = style.font.strike
|
|
141
|
+
if style.font.u:
|
|
142
|
+
font_style_map['UnderLine'] = style.font.u
|
|
143
|
+
if style.font.color.rgb:
|
|
144
|
+
font_style_map['Color'] = f'#{style.font.color.rgb[2:]}'
|
|
145
|
+
return font_style_map
|
|
146
|
+
|
|
147
|
+
def _get_fill_style(self, style: CustomStyle) -> dict[str, str]:
|
|
148
|
+
fill_style_map = {}
|
|
149
|
+
if style.fill.fgColor.rgb:
|
|
150
|
+
fill_style_map['Color'] = f'#{style.fill.fgColor.rgb[2:]}'
|
|
151
|
+
fill_style_map['Type'] = 'pattern'
|
|
152
|
+
fill_style_map['Pattern'] = 1
|
|
153
|
+
return fill_style_map
|
|
154
|
+
|
|
155
|
+
def _get_border_style(self, style: CustomStyle) -> dict[str, str]:
|
|
156
|
+
border_style_map = {}
|
|
157
|
+
direction = ['left', 'right', 'top', 'bottom']
|
|
158
|
+
|
|
159
|
+
for d in direction:
|
|
160
|
+
border_style_map[d] = {}
|
|
161
|
+
|
|
162
|
+
if style.border.left.style:
|
|
163
|
+
border_style_map['left']['Style'] = self.BORDER_TO_INDEX[style.border.left.style]
|
|
164
|
+
if style.border.right.style:
|
|
165
|
+
border_style_map['right']['Style'] = self.BORDER_TO_INDEX[style.border.right.style]
|
|
166
|
+
if style.border.top.style:
|
|
167
|
+
border_style_map['top']['Style'] = self.BORDER_TO_INDEX[style.border.top.style]
|
|
168
|
+
if style.border.bottom.style:
|
|
169
|
+
border_style_map['bottom']['Style'] = self.BORDER_TO_INDEX[style.border.bottom.style]
|
|
170
|
+
|
|
171
|
+
if style.border.left.color.rgb:
|
|
172
|
+
border_style_map['left']['Color'] = f'#{style.border.left.color.rgb[2:]}'
|
|
173
|
+
if style.border.right.color.rgb:
|
|
174
|
+
border_style_map['right']['Color'] = f'#{style.border.right.color.rgb[2:]}'
|
|
175
|
+
if style.border.top.color.rgb:
|
|
176
|
+
border_style_map['top']['Color'] = f'#{style.border.top.color.rgb[2:]}'
|
|
177
|
+
if style.border.bottom.color.rgb:
|
|
178
|
+
border_style_map['bottom']['Color'] = f'#{style.border.bottom.color.rgb[2:]}'
|
|
179
|
+
return border_style_map
|
|
180
|
+
|
|
181
|
+
def _get_alignment_style(self, style: CustomStyle) -> dict[str, str]:
|
|
182
|
+
ali_style_map = {}
|
|
183
|
+
|
|
184
|
+
if style.ali.horizontal:
|
|
185
|
+
ali_style_map['Horizontal'] = style.ali.horizontal
|
|
186
|
+
if style.ali.vertical:
|
|
187
|
+
ali_style_map['Vertical'] = style.ali.vertical
|
|
188
|
+
if style.ali.wrapText:
|
|
189
|
+
ali_style_map['WrapText'] = style.ali.wrapText
|
|
190
|
+
if style.ali.shrinkToFit:
|
|
191
|
+
ali_style_map['ShrinkToFit'] = style.ali.shrinkToFit
|
|
192
|
+
if style.ali.indent:
|
|
193
|
+
ali_style_map['Indent'] = style.ali.indent
|
|
194
|
+
if style.ali.readingOrder:
|
|
195
|
+
ali_style_map['ReadingOrder'] = style.ali.readingOrder
|
|
196
|
+
if style.ali.textRotation:
|
|
197
|
+
ali_style_map['TextRotation'] = style.ali.textRotation
|
|
198
|
+
if style.ali.justifyLastLine:
|
|
199
|
+
ali_style_map['JustifyLastLine'] = style.ali.justifyLastLine
|
|
200
|
+
if style.ali.relativeIndent:
|
|
201
|
+
ali_style_map['RelativeIndent'] = style.ali.relativeIndent
|
|
202
|
+
|
|
203
|
+
return ali_style_map
|
|
204
|
+
|
|
205
|
+
def _get_protection_style(self, style: CustomStyle) -> dict[str, str]:
|
|
206
|
+
protection_style_map = {}
|
|
207
|
+
if style.protection.locked:
|
|
208
|
+
protection_style_map['Locked'] = style.protection.locked
|
|
209
|
+
if style.protection.hidden:
|
|
210
|
+
protection_style_map['Hidden'] = style.protection.hidden
|
|
211
|
+
return protection_style_map
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
class FastWriter(PyExcelizeDriver):
|
|
215
|
+
def __init__(self, data: list[dict[str, str]]):
|
|
216
|
+
super().__init__()
|
|
217
|
+
# The data is list[dict[str, str]] as default, if your data is other dtype
|
|
218
|
+
# You should override the __init___ method to allocate correct space for __row_list
|
|
219
|
+
self._row_list = [[None] * (len(data[0])) for _ in range(len(data))]
|
|
220
|
+
self.data = data
|
|
221
|
+
|
|
222
|
+
def row_append(self, value: str, style: str, row_idx: int, col_idx: int):
|
|
223
|
+
if isinstance(style, CustomStyle):
|
|
224
|
+
style = self.style_map_name[style]
|
|
225
|
+
self._row_list[row_idx][col_idx] = (value, style)
|
|
226
|
+
|
|
227
|
+
def _pop_none_from_row_list(self, idx: int) -> None:
|
|
228
|
+
for i in range(len(self._row_list[idx]) - 1, 0, -1):
|
|
229
|
+
if self._row_list[idx][i] is None:
|
|
230
|
+
self._row_list[idx].pop()
|
|
231
|
+
else:
|
|
232
|
+
break
|
|
233
|
+
|
|
234
|
+
def apply_to_header(self, idx: int = 0):
|
|
235
|
+
original_len = len(self._row_list[idx])
|
|
236
|
+
self._pop_none_from_row_list(idx)
|
|
237
|
+
self.pyexcelize_data[self.sheet]['Header'] = self._row_list[idx]
|
|
238
|
+
# Reset row_list for body creation
|
|
239
|
+
self._row_list[idx] = [None] * original_len
|
|
240
|
+
|
|
241
|
+
def create_row(self, idx):
|
|
242
|
+
self._pop_none_from_row_list(idx)
|
|
243
|
+
self.pyexcelize_data[self.sheet]['Data'].append(self._row_list[idx])
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
class NormalWriter(PyExcelizeDriver):
|
|
247
|
+
def __init__(self, data: list[dict[str, str]]):
|
|
248
|
+
super().__init__()
|
|
249
|
+
self._row_list = []
|
|
250
|
+
self.data = data
|
|
251
|
+
|
|
252
|
+
def row_append(self, value: str, style: str | CustomStyle):
|
|
253
|
+
if isinstance(style, CustomStyle):
|
|
254
|
+
style = self.style_map_name[style]
|
|
255
|
+
self._row_list.append((value, style))
|
|
256
|
+
|
|
257
|
+
def create_row(self, is_header: bool = False):
|
|
258
|
+
key = 'Header' if is_header is True else 'Data'
|
|
259
|
+
self.pyexcelize_data[self.sheet][key].append(self._row_list)
|
|
260
|
+
self._row_list = []
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024, Jian Yu, Chen
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyfastexcel
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: High performace excel file generation library.
|
|
5
|
+
Home-page: https://github.com/Zncl2222/pyfastexcel
|
|
6
|
+
Author: Zncl2222
|
|
7
|
+
Author-email: 3002shinning@gmail.com
|
|
8
|
+
License: BSD-3-Clause
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
11
|
+
Classifier: Programming Language :: Go
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Requires-Python: >=3.7
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: msgspec
|
|
23
|
+
Requires-Dist: openpyxl-style-writer
|
|
24
|
+
|
|
25
|
+
# pyfastexcel
|
|
26
|
+
This package enables high-performance Excel writing by integrating with the streaming API from the golang package [excelize](https://github.com/qax-os/excelize). Users can leverage this functionality without the need to write any Go code, as the entire process can be accomplished through Python.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
```bash
|
|
30
|
+
pip install pyfastexcel
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Introduction
|
|
34
|
+
|
|
35
|
+
Comming soon...
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
Comming soon...
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
pyfastexcel/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
pyfastexcel/driver.py,sha256=AiJP2qWIXG-jJS_aiNmA6z17WSNA3676jCUnDEwVXOk,9969
|
|
3
|
+
pyfastexcel/pyfastexcel.dll,sha256=Ow0zLQ1IUbn2eueMXG4K2FbVNGjnRrSkdEjvXNT6xOU,9653916
|
|
4
|
+
pyfastexcel/pyfastexcel.so,sha256=qYAklVzlYPagfQ3SqIUI7HWbXlYAjQxzkUiAcXAl0Pk,6773624
|
|
5
|
+
pyfastexcel-0.0.1.dist-info/LICENSE,sha256=kBvf8yHo7qOBx-m8Yd0aj6vRanVBcem_VUxNvbqzHpY,1500
|
|
6
|
+
pyfastexcel-0.0.1.dist-info/METADATA,sha256=8htpOAtLqcqGPJN_4FCn5hGgYJGqmyb1xeXtXDB1uG8,1299
|
|
7
|
+
pyfastexcel-0.0.1.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
|
|
8
|
+
pyfastexcel-0.0.1.dist-info/top_level.txt,sha256=OFydmanH9QXbV8Z2zDwxShLA1LbdRLbdbOa8xcTbYBE,12
|
|
9
|
+
pyfastexcel-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyfastexcel
|