pyxlmapper
This library can help reading and mapping hierarchical .xlsx spreadsheets like this one:
into data such as:
[
{
"category": {
"group_a": {
"a": "...",
"b": "..."
},
"group_b": {
"c": "...",
"d": "...",
}
},
"lone_field": "...",
},
...
]
Or this:
[
{
"category.group_a.a": "...",
"category.group_a.b": "...",
"category.group_b.c": "...",
"category.group_b.d": "...",
"lone_field": "...",
},
...
]
Installation
WARNING: PyPI has no package yet. Name clame is pending: https://github.com/pypi/support/issues/5471
pip install pyxlmapper
Usage
You can write your own mapper or use code generation to infer mapper automatically
Writing and using mapper
pyxlmapper uses DSL based on python classes. First, you need to define a mapper class that
inherits SpreadsheetMapper. And then define all headers. Class nesting is resambling headers nesting
in the spreadsheet. You do not need to specify offsets manually, it will be calculated automatically,
considering that header are nested from top to bottom and column order is from left to right without gaps
(by default, but could be modified). For the example from above, the mapper would look like this:
import json
import openpyxl
from pyxlmapper import SpreadsheetMapper
class Mapper(SpreadsheetMapper):
class Category:
class GroupA:
class A:
pass
class B:
pass
class GroupB:
class C:
pass
class D:
pass
class LoneField:
pass
# Open spreadhseet:
wb = openpyxl.open('data.xlsx', data_only=True)
ws = wb['sheet name'] # or ws.active for the first one
# instantiate mapper:
mapper = Mapper()
data = []
for row in mapper.map_rows(ws, start_at=3):
print(row)
# OR
data.append(row)
# Save a file
with open('output.json', 'w') as fd:
fd.write(json.dump(data, fd))
Syntax
Class name
Class names are used to automaticaly derive input_name and output_name if not provided.
input_name would be derived as such: SomeFieldName into Some Field Name. output_name on the
other hand would be derived as some_field_name.
output_name (str)
If specified, used as an override for the name of the field in the output JSON file.
Example:
class Mapper(SpreadsheetMapper):
class SomeField:
output_name = "other_name"
input_name (str)
If specified, used as an override for the column name in the xlsx spreadsheet.
Example:
class Mapper(SpreadsheetMapper):
class SomeField:
input_name = "You can use ::any:: symbols"
offset tuple[int, int]
Specifies an offset, relative to default position. Useful when some column or row is skipped
Format: offset = (v_offset: int, h_offset: int)
class Mapper(SpreadsheetMapper):
class SomeField:
offset = (0, 1) # To skip a column
Metadata
Release files for pyxlmapper 0.0.2.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyxlmapper-0.0.2.4.tar.gz | 39.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyxlmapper-0.0.2.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 50.2 kB
Release files / pyxlmapper-0.0.2.4.tar.gz
| Download URL | pyxlmapper-0.0.2.4.tar.gz |
|---|---|
| Size | 39.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9947617e841e36735d3536b0ae11168a7991a5e3042910f0f00bbb22a1cf6aff
|
|
BLAKE2b-256 checksum How to use checksums |
b174b20a96dd58b7511f15a55c40ee40ce658ab95f9c99aece254fea30cec197
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 3, 2025.
Transparency logRelease files / pyxlmapper-0.0.2.4-py3-none-any.whl
| Download URL | pyxlmapper-0.0.2.4-py3-none-any.whl |
|---|---|
| Size | 10.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9d5a8d084072c0d36bc5f190d851a61a3b39ba90aedd6f47c579138435819309
|
|
BLAKE2b-256 checksum How to use checksums |
62aa174f590dfd341988ccaf890287f5a16de904f378f7abf9ec784b718fa0f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 3, 2025.
Transparency log