A Python Library for Reading and Writing Excel Files
Project description
中文 | English
ExcelAlchemy User Guide
📊 ExcelAlchemy
ExcelAlchemy is a Python library that allows you to download Excel files from Minio, parse user inputs, and generate corresponding Pydantic classes. It also allows you to generate Excel files based on Pydantic classes for easy user downloads.
Installation
Use pip to install:
pip install ExcelAlchemy
Usage
Generate Excel template from Pydantic class
from excelalchemy import ExcelAlchemy, FieldMeta, ImporterConfig, Number, String
from pydantic import BaseModel
class Importer(BaseModel):
age: Number = FieldMeta(label='Age', order=1)
name: String = FieldMeta(label='Name', order=2)
phone: String | None = FieldMeta(label='Phone', order=3)
address: String | None = FieldMeta(label='Address', order=4)
alchemy = ExcelAlchemy(ImporterConfig(Importer))
base64content = alchemy.download_template()
print(base64content)
- The above is a simple example of generating an Excel template from a Pydantic class. The Excel template will have a sheet named "Sheet1" with four columns: "Age", "Name", "Phone", and "Address". "Age" and "Name" are required fields, while "Phone" and "Address" are optional.
- The method returns a base64-encoded string that represents the Excel file. You can directly use the window.open method to open the Excel file in the front-end, or download it by typing the base64 content in the browser's address bar.
- When downloading a template, you can also specify some default values, for example:
from excelalchemy import ExcelAlchemy, FieldMeta, ImporterConfig, Number, String
from pydantic import BaseModel
class Importer(BaseModel):
age: Number = FieldMeta(label='Age', order=1)
name: String = FieldMeta(label='Name', order=2)
phone: String | None = FieldMeta(label='Phone', order=3)
address: String | None = FieldMeta(label='Address', order=4)
alchemy = ExcelAlchemy(ImporterConfig(Importer))
sample = [
{'age': 18, 'name': 'Bob', 'phone': '12345678901', 'address': 'New York'},
{'age': 19, 'name': 'Alice', 'address': 'Shanghai'},
{'age': 20, 'name': 'John', 'phone': '12345678901'},
]
base64content = alchemy.download_template(sample)
print(base64content)
In the above example, we specify a sample, which is a list of dictionaries. Each dictionary represents a row in the Excel sheet, and the keys represent column names. The method returns an Excel template with default values filled in. If a field doesn't have a default value, it will be empty. For example:
Parse a Pydantic class from an Excel file and create data
import asyncio
from typing import Any
from excelalchemy import ExcelAlchemy, FieldMeta, ImporterConfig, Number, String
from minio import Minio
from pydantic import BaseModel
class Importer(BaseModel):
age: Number = FieldMeta(label='Age', order=1)
name: String = FieldMeta(label='Name', order=2)
phone: String | None = FieldMeta(label='Phone', order=3)
address: String | None = FieldMeta(label='Address', order=4)
def data_converter(data: dict[str, Any]) -> dict[str, Any]:
"""Custom data converter, here you can modify the result of Importer.dict()"""
data['age'] = data['age'] + 1
data['name'] = {"phone": data['phone']}
return data
async def create_func(data: dict[str, Any], context: None) -> Any:
"""Your defined creation function"""
# do something to create data
return True
async def main():
alchemy = ExcelAlchemy(
ImporterConfig(
create_importer_model=Importer,
creator=create_func,
data_converter=data_converter,
minio=Minio(endpoint=''), # reachable minio address
bucket_name='excel',
url_expires=3600,
)
)
result = await alchemy.import_data(input_excel_name='test.xlsx', output_excel_name="test.xlsx")
print(result)
asyncio.run(main())
-
The importing function is based on
Minio
, so you need to install Minio and create a bucket to use this functionality for storing the Excel files. -
The imported Excel file must be generated by the
download_template()
method, otherwise, it will produce a parsing error. -
In the above example, we define a
data_converter
function, which is used to modify the result ofImporter.dict().
The final result ofdata_converter
function will be the parameter of the create_func function. This function is optional if you don't need to modify the data. -
The
create_func
function is used to create data, and the parameter is the result of the data_converter function, and context is None. You can create data, for example, by storing the data in a database. -
The
input_excel_name
parameter of theimport_data()
method is the name of the Excel file in Minio, and theoutput_excel_name
parameter is the name of the Excel file with the parsing result in Minio. This file contains all the input data, and if any data fails the parsing, the first column of that data has an error message, and the error-producing cell is highlighted in red. -
The method returns an
ImportResult
type result. You can see the definition of this class in the code. This class contains all the information about the parsing result, such as the number of successfully imported data, the number of failed data, the failed data, etc. -
An example of the importing result is shown in the following image:
Contributing
If you have any questions or suggestions regarding the ExcelAlchemy library, please raise an issue in GitHub Issues. We also welcome you to submit a pull request to contribute your code.
License
ExcelAlchemy is licensed under the MIT license. For more information, please see the LICENSE file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ExcelAlchemy-1.1.0.tar.gz
.
File metadata
- Download URL: ExcelAlchemy-1.1.0.tar.gz
- Upload date:
- Size: 38.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe7ddf895cc36c03b5f7ad0dc9c63ff7ebe3f83dc73b6790e299180365559eab |
|
MD5 | f3649c9290c68906e96cd469d417ff5a |
|
BLAKE2b-256 | 94db07a8bccd5ca745413b216b070201dc970ad90e188e1144fd8bd69a9ee72d |
File details
Details for the file excelalchemy-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: excelalchemy-1.1.0-py3-none-any.whl
- Upload date:
- Size: 52.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b0a6d32ef3e3b52378e390baccb8fcc116fb3bbe88f305f08cf8e93504016c2 |
|
MD5 | 32508cf366206d64b64f428b44a93859 |
|
BLAKE2b-256 | 86b54d575296379e0562169df61e9c1f38629cc3d3acae2415c5214de7d3fe2e |