Skip to main content

Dump/Load Excel files to/from Python objects

Project description

Excel Serializer

Example

Excel Serializer is a Python package that provides a set of functions and classes to serialize and deserialize Python objects to and from Excel files. The API is designed to be intuitive and familiar, closely mirroring the interface of the built-in json module. This makes it easy to configure and use for developers who are already accustomed to working with JSON serialization.

Key Features

  • load: Deserialize an Excel file to a Python object.
  • loadw: Deserialize an openpyxl workbook to a Python object.
  • dump: Serialize a Python object to an Excel file.
  • dumpw: Serialize a Python object to an openpyxl workbook.

Dependencies

  • openpyxl: This module relies on the openpyxl library for reading from and writing to Excel files. Ensure that openpyxl is installed in your environment to use this module.

Installation

You can install the package using pip:

pip install excel-serializer

Builtin types

This module has four builtin types:

  • List: A list of values.
  • Tuple: A tuple of values.
  • Dict: A dictionary of key-value pairs.
  • DictList: A list of dictionaries all having the same keys.

You can easily add your own types by subclassing ExcelEncoder and ExcelDecoder classes. See how to do so in examples below.

Usage

Encoding basic Python object hierarchies

import excel_serializer as es

data = {'name': 'John', 'age': 30, 'city': 'New York'}
es.dump(data, 'data.xlsx')

Decoding Excel files

import excel_serializer as es

data = es.load('data.xlsx')
print(data)
# Output: {'name': 'John', 'age': 30, 'city': 'New York'}

Using a custom encoder

You can either convert the custom object to a built-in type:

import excel_serializer as es

class CustomEncoder(es.ExcelEncoder):
    def default(self, obj):
        if isinstance(obj, set):
            return list(obj)
        return super()._default(obj)

data = {'numbers': {1, 2, 3}}
es.dump(data, 'data.xlsx', cls=CustomEncoder)

or implement a custom encoder to handle the serialization of the custom object:

import excel_serializer as es

class CustomEncoder(es.ExcelEncoder):
    def write_set(self, sheet, type_cell, st):
        cols = ('Value',)
        type_cell.value = f'Set {type_cell.value}'
        sheet.append((type_cell,))
        sheet.append(cols)
        for i, e in enumerate(st):
            sheet.append((i + 1, self.encode(sheet, i + 3, 2, str(i + 1), e)))
        return 2 + len(st), 1, cols
    
    def write_custom_type(self, sheet, type_cell, obj):
        if isinstance(obj, set):
            return self.write_set(sheet, type_cell, obj)
        return super().write_custom_type(sheet, type_cell, obj)

data = {'numbers': {1, 2, 3}}
es.dump(data, 'data.xlsx', cls=CustomEncoder)

Using a custom decoder

import excel_serializer as es

class CustomDecoder(es.ExcelDecoder):
    def read_set(self, sheet_name, rows):
        headers = next(rows)
        if len(headers) != 1:
            raise es.ExcelDecodeError(f'Invalid list headers. Expected 1, found {len(headers)}',
                                   self.workbook, sheet_name, 2, len(headers) + 1)
        if headers[0].value != 'Value':
            raise es.ExcelDecodeError(f'Invalid list headers. Expected "Value", found "{headers[0].value}"',
                                      self.workbook, sheet_name, 2, 1)
        return set(self.read_value(row[0]) for row in rows)
    
    def read_custom_type(self, sheet_type, sheet_name, rows):
        if sheet_type == 'Set':
            return self.read_set(sheet_name, rows)
        return super().read_custom_type(sheet_type, sheet_name, rows)

data = es.load('data.xlsx', cls=CustomDecoder)
print(data)
# Output: {'numbers': {1, 2, 3}}

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

Alexandre 'Tsu' Manuel - tsu@sulvia.fr

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

excel_serializer-1.1.3.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

excel_serializer-1.1.3-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file excel_serializer-1.1.3.tar.gz.

File metadata

  • Download URL: excel_serializer-1.1.3.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for excel_serializer-1.1.3.tar.gz
Algorithm Hash digest
SHA256 bd5ad2a5f851adfbb0cda254449cb49fe0e12728045d66011664c926c3fd70d1
MD5 6a3277c5f2889b777c51fcc2d9427bb4
BLAKE2b-256 f1f185508634db21fc7bace9cc6e16ba6a1ef187da9b78ec5b3e9da0301c2fd9

See more details on using hashes here.

File details

Details for the file excel_serializer-1.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for excel_serializer-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 40f54fcaa0dd520b79f7c88b7b2d912e74234e4152dbe6eff52aec3dd4224f65
MD5 8c661d3f747aefe5a12b2d58b0615e2a
BLAKE2b-256 b3876939eb392927d4cdb1c14a0ea427d42564ab77b9dfa268f1aa0f2eb274d3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page