pyfastexcel 0.0.6__tar.gz → 0.0.8__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.
- pyfastexcel-0.0.8/PKG-INFO +194 -0
- pyfastexcel-0.0.8/README.md +170 -0
- pyfastexcel-0.0.8/pyfastexcel/__init__.py +13 -0
- pyfastexcel-0.0.8/pyfastexcel/driver.py +186 -0
- {pyfastexcel-0.0.6 → pyfastexcel-0.0.8}/pyfastexcel/pyfastexcel.dll +0 -0
- {pyfastexcel-0.0.6 → pyfastexcel-0.0.8}/pyfastexcel/pyfastexcel.so +0 -0
- pyfastexcel-0.0.8/pyfastexcel/style.py +198 -0
- pyfastexcel-0.0.8/pyfastexcel/utils.py +115 -0
- pyfastexcel-0.0.8/pyfastexcel/workbook.py +124 -0
- pyfastexcel-0.0.8/pyfastexcel/worksheet.py +407 -0
- pyfastexcel-0.0.8/pyfastexcel/writer.py +60 -0
- pyfastexcel-0.0.8/pyfastexcel.egg-info/PKG-INFO +194 -0
- {pyfastexcel-0.0.6 → pyfastexcel-0.0.8}/pyfastexcel.egg-info/SOURCES.txt +3 -1
- {pyfastexcel-0.0.6 → pyfastexcel-0.0.8}/setup.cfg +1 -1
- pyfastexcel-0.0.6/PKG-INFO +0 -346
- pyfastexcel-0.0.6/README.md +0 -322
- pyfastexcel-0.0.6/pyfastexcel/__init__.py +0 -7
- pyfastexcel-0.0.6/pyfastexcel/driver.py +0 -631
- pyfastexcel-0.0.6/pyfastexcel/exceptions.py +0 -13
- pyfastexcel-0.0.6/pyfastexcel/utils.py +0 -78
- pyfastexcel-0.0.6/pyfastexcel/writer.py +0 -263
- pyfastexcel-0.0.6/pyfastexcel.egg-info/PKG-INFO +0 -346
- {pyfastexcel-0.0.6 → pyfastexcel-0.0.8}/LICENSE +0 -0
- {pyfastexcel-0.0.6 → pyfastexcel-0.0.8}/pyfastexcel.egg-info/dependency_links.txt +0 -0
- {pyfastexcel-0.0.6 → pyfastexcel-0.0.8}/pyfastexcel.egg-info/not-zip-safe +0 -0
- {pyfastexcel-0.0.6 → pyfastexcel-0.0.8}/pyfastexcel.egg-info/requires.txt +0 -0
- {pyfastexcel-0.0.6 → pyfastexcel-0.0.8}/pyfastexcel.egg-info/top_level.txt +0 -0
- {pyfastexcel-0.0.6 → pyfastexcel-0.0.8}/setup.py +0 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyfastexcel
|
|
3
|
+
Version: 0.0.8
|
|
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: MIT
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT 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>=1.1.3
|
|
24
|
+
|
|
25
|
+
# pyfastexcel
|
|
26
|
+
|
|
27
|
+

|
|
28
|
+
[](https://goreportcard.com/report/github.com/Zncl2222/pyfastexcel)
|
|
29
|
+

|
|
30
|
+

|
|
31
|
+
[](https://app.codacy.com/gh/Zncl2222/pyfastexcel/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
|
|
32
|
+
[](https://codecov.io/gh/Zncl2222/pyfastexcel)
|
|
33
|
+
[](https://pyfastexcel.readthedocs.io/en/stable/?badge=stable)
|
|
34
|
+
|
|
35
|
+
This package enables high-performance Excel writing by integrating with the
|
|
36
|
+
streaming API from the golang package
|
|
37
|
+
[excelize](https://github.com/qax-os/excelize). Users can leverage this
|
|
38
|
+
functionality without the need to write any Go code, as the entire process
|
|
39
|
+
can be accomplished through Python.
|
|
40
|
+
|
|
41
|
+
## Features
|
|
42
|
+
|
|
43
|
+
- Python and Golang Integration: Seamlessly call Golang built shared
|
|
44
|
+
libraries from Python.
|
|
45
|
+
|
|
46
|
+
- No Golang Code Required: Users can solely rely on Python for Excel file
|
|
47
|
+
generation, eliminating the need for Golang expertise.
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
### Install via pip (Recommended)
|
|
52
|
+
|
|
53
|
+
You can easily install the package via pip
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install pyfastexcel
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Install manually
|
|
60
|
+
|
|
61
|
+
If you prefer to build the package manually, follow these steps:
|
|
62
|
+
|
|
63
|
+
1. Clone the repository:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
git clone https://github.com/Zncl2222/pyfastexcel.git
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
2. Go to the project root directory:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
cd pyfastexcel
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
3. Install the required golang packages:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
go mod download
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
4. Build the Golang shared library using the Makefile:
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
make
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
5. Install the required python packages:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
pip install -r requirements.txt
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
or
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
pipenv install
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
6. Import the project and start using it!
|
|
100
|
+
|
|
101
|
+
## Usage
|
|
102
|
+
|
|
103
|
+
The index assignment is now avaliable in `Workbook` and the `FastWriter`.
|
|
104
|
+
Here is the example usage:
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from pyfastexcel import Workbook
|
|
108
|
+
from pyfastexcel.utils import set_custom_style
|
|
109
|
+
|
|
110
|
+
# CustomStyle will be integrate to the pyfatexcel in next version
|
|
111
|
+
# Beside, CustomStyle will be re-implement in future to make it no-longer
|
|
112
|
+
# depend on openpyxl_style writer and openpyxl
|
|
113
|
+
from openpyxl_style_writer import CustomStyle
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
if __name__ == '__main__':
|
|
117
|
+
# Workbook
|
|
118
|
+
wb = Workbook()
|
|
119
|
+
|
|
120
|
+
# Set and register CustomStyle
|
|
121
|
+
bold_style = CustomStyle(font_size=15, font_bold=True)
|
|
122
|
+
set_custom_style('bold_style', bold_style)
|
|
123
|
+
|
|
124
|
+
ws = wb['Sheet1']
|
|
125
|
+
# Write value with default style
|
|
126
|
+
ws['A1'] = 'A1 value'
|
|
127
|
+
# Write value with custom style
|
|
128
|
+
ws['B1'] = ('B1 value', 'bold_style')
|
|
129
|
+
|
|
130
|
+
# Write value in slice with default style
|
|
131
|
+
ws['A2': 'C2'] = [1, 2, 3]
|
|
132
|
+
# Write value in slice with custom style
|
|
133
|
+
ws['A3': 'C3'] = [(1, 'bold_style'), (2, 'bold_style'), (3, 'bold_style')]
|
|
134
|
+
|
|
135
|
+
# Write value by row with default style (python index 0 is the index 1 in excel)
|
|
136
|
+
ws[3] = [9, 8, 'go']
|
|
137
|
+
# Write value by row with custom style
|
|
138
|
+
ws[4] = [(9, 'bold_style'), (8, 'bold_style'), ('go', 'bold_style')]
|
|
139
|
+
|
|
140
|
+
# Send request to golang lib and create excel
|
|
141
|
+
wb.read_lib_and_create_excel()
|
|
142
|
+
|
|
143
|
+
# File path to save
|
|
144
|
+
file_path = 'pyexample_workbook.xlsx'
|
|
145
|
+
wb.save(file_path)
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
You can also using the `FastWriter` or `NormalWriter` which was the
|
|
150
|
+
subclass of `Workbook` to write excel row by row, see the more in documentations [quickstart](https://pyfastexcel.readthedocs.io/en/stable/quickstart/).
|
|
151
|
+
|
|
152
|
+
## Documentation
|
|
153
|
+
|
|
154
|
+
The documentation is hosted on Read the Docs.
|
|
155
|
+
|
|
156
|
+
- [Development Version](https://pyfastexcel.readthedocs.io/en/latest/)
|
|
157
|
+
|
|
158
|
+
- [Latest Stable Version](https://pyfastexcel.readthedocs.io/en/stable/)
|
|
159
|
+
|
|
160
|
+
## Benchmark
|
|
161
|
+
|
|
162
|
+
The following result displays the performance comparison between
|
|
163
|
+
`pyfastexcel` and `openpyxl` for writing 50000 rows with 30
|
|
164
|
+
columns (Total 1500000 cells). To see more benchmark results, please
|
|
165
|
+
see the [benchmark](https://pyfastexcel.readthedocs.io/en/stable/benchmark/).
|
|
166
|
+
|
|
167
|
+
<dev align='center'>
|
|
168
|
+
<img src='./docs/images/50000_30_horizontal_Windows11.png' width="80%" height="45%" >
|
|
169
|
+
</dev>
|
|
170
|
+
|
|
171
|
+
## How it Works
|
|
172
|
+
|
|
173
|
+
The core functionality revolves around encoding Excel cell data and styles,
|
|
174
|
+
or any other Excel properties, into a JSON string within Python. This JSON
|
|
175
|
+
payload is then passed through ctypes to a Golang shared library. In Golang,
|
|
176
|
+
the JSON is parsed, and using the streaming writer of
|
|
177
|
+
[excelize](https://github.com/qax-os/excelize) to wrtie excel in
|
|
178
|
+
high performance.
|
|
179
|
+
|
|
180
|
+
## Current Limitations & Future Plans
|
|
181
|
+
|
|
182
|
+
### Problem 1: Dependence on Other Excel Package
|
|
183
|
+
|
|
184
|
+
Limitations:
|
|
185
|
+
|
|
186
|
+
This project currently depends on the `CustomStyle` object of
|
|
187
|
+
the [openpyxl_style_writer](https://github.com/Zncl2222/openpyxl_style_writer)
|
|
188
|
+
package, which is built for openpyxl to write styles in write-only
|
|
189
|
+
mode more efficiently without duplicating code.
|
|
190
|
+
|
|
191
|
+
Future Plans:
|
|
192
|
+
|
|
193
|
+
This project plans to create its own `Style` object, making it no longer
|
|
194
|
+
dependent on the mentioned package.
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# pyfastexcel
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
[](https://goreportcard.com/report/github.com/Zncl2222/pyfastexcel)
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
[](https://app.codacy.com/gh/Zncl2222/pyfastexcel/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
|
|
8
|
+
[](https://codecov.io/gh/Zncl2222/pyfastexcel)
|
|
9
|
+
[](https://pyfastexcel.readthedocs.io/en/stable/?badge=stable)
|
|
10
|
+
|
|
11
|
+
This package enables high-performance Excel writing by integrating with the
|
|
12
|
+
streaming API from the golang package
|
|
13
|
+
[excelize](https://github.com/qax-os/excelize). Users can leverage this
|
|
14
|
+
functionality without the need to write any Go code, as the entire process
|
|
15
|
+
can be accomplished through Python.
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- Python and Golang Integration: Seamlessly call Golang built shared
|
|
20
|
+
libraries from Python.
|
|
21
|
+
|
|
22
|
+
- No Golang Code Required: Users can solely rely on Python for Excel file
|
|
23
|
+
generation, eliminating the need for Golang expertise.
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
### Install via pip (Recommended)
|
|
28
|
+
|
|
29
|
+
You can easily install the package via pip
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install pyfastexcel
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Install manually
|
|
36
|
+
|
|
37
|
+
If you prefer to build the package manually, follow these steps:
|
|
38
|
+
|
|
39
|
+
1. Clone the repository:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
git clone https://github.com/Zncl2222/pyfastexcel.git
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
2. Go to the project root directory:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
cd pyfastexcel
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
3. Install the required golang packages:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
go mod download
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
4. Build the Golang shared library using the Makefile:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
make
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
5. Install the required python packages:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
pip install -r requirements.txt
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
or
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pipenv install
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
6. Import the project and start using it!
|
|
76
|
+
|
|
77
|
+
## Usage
|
|
78
|
+
|
|
79
|
+
The index assignment is now avaliable in `Workbook` and the `FastWriter`.
|
|
80
|
+
Here is the example usage:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from pyfastexcel import Workbook
|
|
84
|
+
from pyfastexcel.utils import set_custom_style
|
|
85
|
+
|
|
86
|
+
# CustomStyle will be integrate to the pyfatexcel in next version
|
|
87
|
+
# Beside, CustomStyle will be re-implement in future to make it no-longer
|
|
88
|
+
# depend on openpyxl_style writer and openpyxl
|
|
89
|
+
from openpyxl_style_writer import CustomStyle
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
if __name__ == '__main__':
|
|
93
|
+
# Workbook
|
|
94
|
+
wb = Workbook()
|
|
95
|
+
|
|
96
|
+
# Set and register CustomStyle
|
|
97
|
+
bold_style = CustomStyle(font_size=15, font_bold=True)
|
|
98
|
+
set_custom_style('bold_style', bold_style)
|
|
99
|
+
|
|
100
|
+
ws = wb['Sheet1']
|
|
101
|
+
# Write value with default style
|
|
102
|
+
ws['A1'] = 'A1 value'
|
|
103
|
+
# Write value with custom style
|
|
104
|
+
ws['B1'] = ('B1 value', 'bold_style')
|
|
105
|
+
|
|
106
|
+
# Write value in slice with default style
|
|
107
|
+
ws['A2': 'C2'] = [1, 2, 3]
|
|
108
|
+
# Write value in slice with custom style
|
|
109
|
+
ws['A3': 'C3'] = [(1, 'bold_style'), (2, 'bold_style'), (3, 'bold_style')]
|
|
110
|
+
|
|
111
|
+
# Write value by row with default style (python index 0 is the index 1 in excel)
|
|
112
|
+
ws[3] = [9, 8, 'go']
|
|
113
|
+
# Write value by row with custom style
|
|
114
|
+
ws[4] = [(9, 'bold_style'), (8, 'bold_style'), ('go', 'bold_style')]
|
|
115
|
+
|
|
116
|
+
# Send request to golang lib and create excel
|
|
117
|
+
wb.read_lib_and_create_excel()
|
|
118
|
+
|
|
119
|
+
# File path to save
|
|
120
|
+
file_path = 'pyexample_workbook.xlsx'
|
|
121
|
+
wb.save(file_path)
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
You can also using the `FastWriter` or `NormalWriter` which was the
|
|
126
|
+
subclass of `Workbook` to write excel row by row, see the more in documentations [quickstart](https://pyfastexcel.readthedocs.io/en/stable/quickstart/).
|
|
127
|
+
|
|
128
|
+
## Documentation
|
|
129
|
+
|
|
130
|
+
The documentation is hosted on Read the Docs.
|
|
131
|
+
|
|
132
|
+
- [Development Version](https://pyfastexcel.readthedocs.io/en/latest/)
|
|
133
|
+
|
|
134
|
+
- [Latest Stable Version](https://pyfastexcel.readthedocs.io/en/stable/)
|
|
135
|
+
|
|
136
|
+
## Benchmark
|
|
137
|
+
|
|
138
|
+
The following result displays the performance comparison between
|
|
139
|
+
`pyfastexcel` and `openpyxl` for writing 50000 rows with 30
|
|
140
|
+
columns (Total 1500000 cells). To see more benchmark results, please
|
|
141
|
+
see the [benchmark](https://pyfastexcel.readthedocs.io/en/stable/benchmark/).
|
|
142
|
+
|
|
143
|
+
<dev align='center'>
|
|
144
|
+
<img src='./docs/images/50000_30_horizontal_Windows11.png' width="80%" height="45%" >
|
|
145
|
+
</dev>
|
|
146
|
+
|
|
147
|
+
## How it Works
|
|
148
|
+
|
|
149
|
+
The core functionality revolves around encoding Excel cell data and styles,
|
|
150
|
+
or any other Excel properties, into a JSON string within Python. This JSON
|
|
151
|
+
payload is then passed through ctypes to a Golang shared library. In Golang,
|
|
152
|
+
the JSON is parsed, and using the streaming writer of
|
|
153
|
+
[excelize](https://github.com/qax-os/excelize) to wrtie excel in
|
|
154
|
+
high performance.
|
|
155
|
+
|
|
156
|
+
## Current Limitations & Future Plans
|
|
157
|
+
|
|
158
|
+
### Problem 1: Dependence on Other Excel Package
|
|
159
|
+
|
|
160
|
+
Limitations:
|
|
161
|
+
|
|
162
|
+
This project currently depends on the `CustomStyle` object of
|
|
163
|
+
the [openpyxl_style_writer](https://github.com/Zncl2222/openpyxl_style_writer)
|
|
164
|
+
package, which is built for openpyxl to write styles in write-only
|
|
165
|
+
mode more efficiently without duplicating code.
|
|
166
|
+
|
|
167
|
+
Future Plans:
|
|
168
|
+
|
|
169
|
+
This project plans to create its own `Style` object, making it no longer
|
|
170
|
+
dependent on the mentioned package.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from openpyxl_style_writer import CustomStyle, DefaultStyle
|
|
2
|
+
|
|
3
|
+
from pyfastexcel.workbook import Workbook
|
|
4
|
+
from pyfastexcel.writer import StreamWriter
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
'Workbook',
|
|
8
|
+
'StreamWriter',
|
|
9
|
+
# Temporary link the CustomStyle from openpyxl_style_writer for
|
|
10
|
+
# convinent usage.
|
|
11
|
+
'CustomStyle',
|
|
12
|
+
'DefaultStyle',
|
|
13
|
+
]
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import ctypes
|
|
5
|
+
import sys
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import msgspec
|
|
10
|
+
from openpyxl_style_writer import CustomStyle
|
|
11
|
+
|
|
12
|
+
from .style import StyleManager
|
|
13
|
+
from .worksheet import WorkSheet
|
|
14
|
+
|
|
15
|
+
BASE_DIR = Path(__file__).resolve().parent
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ExcelDriver:
|
|
19
|
+
"""
|
|
20
|
+
A driver class to write data to Excel files using custom styles.
|
|
21
|
+
|
|
22
|
+
### Attributes:
|
|
23
|
+
BORDER_TO_INDEX (dict[str, int]): Mapping of border styles to excelize's
|
|
24
|
+
corresponding index.
|
|
25
|
+
_FILE_PROPS (dict[str, str]): Default file properties for the Excel
|
|
26
|
+
file.
|
|
27
|
+
|
|
28
|
+
### Methods:
|
|
29
|
+
__init__(): Initializes the ExcelDriver.
|
|
30
|
+
_read_lib(lib_path: str): Reads a library for Excel manipulation.
|
|
31
|
+
read_lib_and_create_excel(lib_path: str = None): Reads the library and
|
|
32
|
+
creates the Excel file.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
_FILE_PROPS = {
|
|
36
|
+
'Category': '',
|
|
37
|
+
'ContentStatus': '',
|
|
38
|
+
'Created': '',
|
|
39
|
+
'Creator': 'pyfastexcel',
|
|
40
|
+
'Description': '',
|
|
41
|
+
'Identifier': 'xlsx',
|
|
42
|
+
'Keywords': 'spreadsheet',
|
|
43
|
+
'LastModifiedBy': 'pyfastexcel',
|
|
44
|
+
'Modified': '',
|
|
45
|
+
'Revision': '0',
|
|
46
|
+
'Subject': '',
|
|
47
|
+
'Title': '',
|
|
48
|
+
'Language': 'en-Us',
|
|
49
|
+
'Version': '',
|
|
50
|
+
}
|
|
51
|
+
_PROTECT_ALGORITHM = (
|
|
52
|
+
'XOR',
|
|
53
|
+
'MD4',
|
|
54
|
+
'MD5',
|
|
55
|
+
'SHA-1',
|
|
56
|
+
'SHA-256',
|
|
57
|
+
'SHA-384',
|
|
58
|
+
'SHA-512',
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def __init__(self):
|
|
62
|
+
"""
|
|
63
|
+
Initializes the PyExcelizeDriver.
|
|
64
|
+
|
|
65
|
+
It initializes the Excel data, file properties, default sheet,
|
|
66
|
+
current sheet, and style mappings.
|
|
67
|
+
"""
|
|
68
|
+
self.workbook = {
|
|
69
|
+
'Sheet1': WorkSheet(),
|
|
70
|
+
}
|
|
71
|
+
self.file_props = self._get_default_file_props()
|
|
72
|
+
self.sheet = 'Sheet1'
|
|
73
|
+
self._sheet_list = tuple(['Sheet1'])
|
|
74
|
+
self._dict_wb = {}
|
|
75
|
+
self.protection = {}
|
|
76
|
+
self.style = StyleManager()
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def sheet_list(self):
|
|
80
|
+
return self._sheet_list
|
|
81
|
+
|
|
82
|
+
def save(self, path: str = './pyfastexcel.xlsx') -> None:
|
|
83
|
+
if not hasattr(self, 'decoded_bytes'):
|
|
84
|
+
self.read_lib_and_create_excel()
|
|
85
|
+
|
|
86
|
+
with open(path, 'wb') as file:
|
|
87
|
+
file.write(self.decoded_bytes)
|
|
88
|
+
|
|
89
|
+
def __getitem__(self, key: str) -> WorkSheet:
|
|
90
|
+
return self.workbook[key]
|
|
91
|
+
|
|
92
|
+
def _check_if_sheet_exists(self, sheet_name: str) -> None:
|
|
93
|
+
if sheet_name not in self.sheet_list:
|
|
94
|
+
raise KeyError(f'{sheet_name} Sheet Does Not Exist.')
|
|
95
|
+
|
|
96
|
+
def read_lib_and_create_excel(self, lib_path: str = None) -> bytes:
|
|
97
|
+
"""
|
|
98
|
+
Reads the library and creates the Excel file.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
lib_path (str, optional): The path to the library. Defaults to None.
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
bytes: The byte data of the created Excel file.
|
|
105
|
+
"""
|
|
106
|
+
pyfastexcel = self._read_lib(lib_path)
|
|
107
|
+
self._create_style()
|
|
108
|
+
|
|
109
|
+
# Transfer all WorkSheet Object to the sheet dictionary in the workbook.
|
|
110
|
+
for sheet in self._sheet_list:
|
|
111
|
+
self._dict_wb[sheet] = self.workbook[sheet]._transfer_to_dict()
|
|
112
|
+
|
|
113
|
+
results = {
|
|
114
|
+
'content': self._dict_wb,
|
|
115
|
+
'file_props': self.file_props,
|
|
116
|
+
'style': self.style._style_map,
|
|
117
|
+
'protection': self.protection,
|
|
118
|
+
}
|
|
119
|
+
json_data = msgspec.json.encode(results)
|
|
120
|
+
create_excel = pyfastexcel.Export
|
|
121
|
+
free_pointer = pyfastexcel.FreeCPointer
|
|
122
|
+
free_pointer.argtypes = [ctypes.c_void_p]
|
|
123
|
+
create_excel.argtypes = [ctypes.c_char_p]
|
|
124
|
+
create_excel.restype = ctypes.c_void_p
|
|
125
|
+
byte_data = create_excel(json_data)
|
|
126
|
+
self.decoded_bytes = base64.b64decode(ctypes.cast(byte_data, ctypes.c_char_p).value)
|
|
127
|
+
free_pointer(byte_data)
|
|
128
|
+
StyleManager.reset_style_configs()
|
|
129
|
+
return self.decoded_bytes
|
|
130
|
+
|
|
131
|
+
def _read_lib(self, lib_path: str) -> ctypes.CDLL:
|
|
132
|
+
"""
|
|
133
|
+
Reads a shared-library for writing Excel.
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
lib_path (str): The path to the library.
|
|
137
|
+
|
|
138
|
+
Returns:
|
|
139
|
+
ctypes.CDLL: The library object.
|
|
140
|
+
"""
|
|
141
|
+
if lib_path is None:
|
|
142
|
+
if sys.platform.startswith('linux'):
|
|
143
|
+
lib_path = str(list(BASE_DIR.glob('**/*.so'))[0])
|
|
144
|
+
elif sys.platform.startswith('win32'):
|
|
145
|
+
lib_path = str(list(BASE_DIR.glob('**/*.dll'))[0])
|
|
146
|
+
lib = ctypes.CDLL(lib_path, winmode=0)
|
|
147
|
+
return lib
|
|
148
|
+
|
|
149
|
+
def _get_default_file_props(self) -> dict[str, str]:
|
|
150
|
+
now = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
|
151
|
+
file_props = self._FILE_PROPS.copy()
|
|
152
|
+
file_props['Created'] = now
|
|
153
|
+
file_props['Modified'] = now
|
|
154
|
+
return file_props
|
|
155
|
+
|
|
156
|
+
def _get_style_collections(self) -> dict[str, CustomStyle]:
|
|
157
|
+
"""
|
|
158
|
+
Gets collections of custom styles.
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
dict[str, CustomStyle]: A dictionary containing custom style
|
|
162
|
+
collections.
|
|
163
|
+
"""
|
|
164
|
+
return {
|
|
165
|
+
attr: getattr(self, attr)
|
|
166
|
+
for attr in dir(self)
|
|
167
|
+
if not callable(getattr(self, attr)) and isinstance(getattr(self, attr), CustomStyle)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
def _create_style(self) -> None:
|
|
171
|
+
"""
|
|
172
|
+
Creates custom styles for the Excel file.
|
|
173
|
+
|
|
174
|
+
This method initializes custom styles for the Excel file based on
|
|
175
|
+
predefined attributes.
|
|
176
|
+
"""
|
|
177
|
+
style_collections = self._get_style_collections()
|
|
178
|
+
self.style._STYLE_NAME_MAP.update({val: key for key, val in style_collections.items()})
|
|
179
|
+
|
|
180
|
+
# Set the CustomStyle from the pre-defined class attributes.
|
|
181
|
+
for key, val in style_collections.items():
|
|
182
|
+
self.style._update_style_map(key, val)
|
|
183
|
+
|
|
184
|
+
# Set the CustomStyle from the REGISTERED method.
|
|
185
|
+
for key, val in self.style.REGISTERED_STYLES.items():
|
|
186
|
+
self.style._update_style_map(key, val)
|
|
Binary file
|
|
Binary file
|