Mapper and parser for hierarchical spreadsheets
Project description
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
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 pyxlmapper-0.0.2.4.tar.gz
.
File metadata
- Download URL: pyxlmapper-0.0.2.4.tar.gz
- Upload date:
- Size: 39.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
9947617e841e36735d3536b0ae11168a7991a5e3042910f0f00bbb22a1cf6aff
|
|
MD5 |
98b332aed9354485e691f33accbb7d28
|
|
BLAKE2b-256 |
b174b20a96dd58b7511f15a55c40ee40ce658ab95f9c99aece254fea30cec197
|
Provenance
The following attestation bundles were made for pyxlmapper-0.0.2.4.tar.gz
:
Publisher:
python-pypi.yml
on unrealsolver/pyxlmapper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
pyxlmapper-0.0.2.4.tar.gz
-
Subject digest:
9947617e841e36735d3536b0ae11168a7991a5e3042910f0f00bbb22a1cf6aff
- Sigstore transparency entry: 206507548
- Sigstore integration time:
-
Permalink:
unrealsolver/pyxlmapper@d6d76fabcbe5d829a424d4e8b32d2edb543edfb3
-
Branch / Tag:
refs/heads/main
- Owner: https://github.com/unrealsolver
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
python-pypi.yml@d6d76fabcbe5d829a424d4e8b32d2edb543edfb3
-
Trigger Event:
push
-
Statement type:
File details
Details for the file pyxlmapper-0.0.2.4-py3-none-any.whl
.
File metadata
- Download URL: pyxlmapper-0.0.2.4-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
9d5a8d084072c0d36bc5f190d851a61a3b39ba90aedd6f47c579138435819309
|
|
MD5 |
49a5fed2c3b8fda64544bc40b1fade95
|
|
BLAKE2b-256 |
62aa174f590dfd341988ccaf890287f5a16de904f378f7abf9ec784b718fa0f5
|
Provenance
The following attestation bundles were made for pyxlmapper-0.0.2.4-py3-none-any.whl
:
Publisher:
python-pypi.yml
on unrealsolver/pyxlmapper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
pyxlmapper-0.0.2.4-py3-none-any.whl
-
Subject digest:
9d5a8d084072c0d36bc5f190d851a61a3b39ba90aedd6f47c579138435819309
- Sigstore transparency entry: 206507554
- Sigstore integration time:
-
Permalink:
unrealsolver/pyxlmapper@d6d76fabcbe5d829a424d4e8b32d2edb543edfb3
-
Branch / Tag:
refs/heads/main
- Owner: https://github.com/unrealsolver
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
python-pypi.yml@d6d76fabcbe5d829a424d4e8b32d2edb543edfb3
-
Trigger Event:
push
-
Statement type: