Simple XLSX and CSV to dictionary converter
Project description
sheet2dict
A simple XLSX/CSV reader - to dictionary converter
Installing
To install the package from pip, first run:
python3 -m pip install --no-cache-dir sheet2dict
Required pip packages for sheet2doc: csv, openpyxl
Usage
This library has 2 main features: reading a spreadsheet files and converting them to array of python dictionaries.
- XLSX
Use xlsx_to_dict()
method when converting form spreadsheets.
Supported file formats for spreadsheets are: .xlsx,.xlsm,.xltx,.xltm
Spreadsheets with multiple worksheets are supported. If no sheet is specified, the active sheet is selected. If there is only one sheet, it is considered active.
# Import the library
from sheet2dict import Worksheet
# Create an object
ws = Worksheet()
# Convert active sheet (without specifying sheet name)
ws.xlsx_to_dict(path='inventory.xlsx')
# Convert the 'Main Warehouse' sheet of the 'inventory.xslx' spreadsheet file.
ws.xlsx_to_dict(path='inventory.xlsx', select_sheet='Main Warehouse')
# object.header returns first row with the data in a spreadsheet
print(ws.header)
# object.sheet_items returns converted rows as dictionaries in the array
print(ws.sheet_items)
You can parse data when worksheet is an object
# Import the library
from sheet2dict import Worksheet
# Example: read spreadsheet as object
path = 'inventory.xlsx'
xlsx_file = open(path, 'rb')
xlsx_file = BytesIO(xlsx_file.read())
# Parse spreadsheet from object
ws = Worksheet()
ws.xlsx_to_dict(path=xlsx_file)
print(ws.header)
- CSV
Use csv_to_dict()
method when converting form csv.
CSV is a format with many variations, better handle encodings and delimiters on user side and not within module itself.
# Import the library
from sheet2dict import Worksheet
# Create an object
ws = Worksheet()
# Read CSV file
csv_file = open('inventory.csv', 'r', encoding='utf-8-sig')
# Convert
ws.csv_to_dict(csv_file=csv_file, delimiter=';')
# object.header returns first row with the data in a spreadsheet
print(ws.header)
# object.sheet_items returns converted rows as dictionaries in the array
print(ws.sheet_items)
- Other functions
Worksheet object.header returns first row with the data in a spreadsheet
Python 3.9.1
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
>>> from sheet2dict import Worksheet
>>> ws = Worksheet()
>>> ws.xlsx_to_dict(path="inventory.xlsx")
>>> ws.header
{'country': 'SK', 'city': 'Bratislava', 'citizens': '400000', 'random_field': 'cc'}
Worksheet object.sanitize_sheet_items removes None or empty dictionary keys from sheet_items
>>> from sheet2dict import Worksheet
>>> ws = Worksheet()
>>> ws.xlsx_to_dict(path="inventory.xlsx")
>>> ws.sheet_items
[
{'country': 'CZ', 'city': 'Prague', 'citizens': '600000', None: '22', 'random_field': 'cc'},
{'country': 'UK', 'city': 'London', 'citizens': '2000000', None: '33', 'random_field': 'cc'}
]
>>> ws.sanitize_sheet_items
[
{'country': 'CZ', 'city': 'Prague', 'citizens': '600000', 'random_field': 'cc'},
{'country': 'UK', 'city': 'London', 'citizens': '2000000', 'random_field': 'cc'}
]
Contributing and Code of Conduct
Contributing to sheet2dict
As an open source project, sheet2dict welcomes contributions of many forms.
Please read and follow our Contributing to sheet2dict
Contributors:
- Thanks to 白一百 (bái-yī-bǎi) for making sheet2dict work with multi-sheet Excel files.
Code of Conduct
As a contributor, you can help us keep the sheet2dict project open and inclusive.
Please read and follow our Code of Conduct
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 sheet2dict-0.1.1.tar.gz
.
File metadata
- Download URL: sheet2dict-0.1.1.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72e8cd0cc128a9007ed71e05efb569ae84d3c82e7f6d2e08e09a2c228548a73e |
|
MD5 | f63283c1e613e51436f6c5427a7a51f2 |
|
BLAKE2b-256 | 9406ba8cb3cc020a6437f55db02b6f2f93fcf8d17b9f3fe4ced446b115a10f6d |
File details
Details for the file sheet2dict-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: sheet2dict-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c1802d3bf7937d4f89802e8925c0c66a7de33b65b6a4e9366f148bcc5028881 |
|
MD5 | 2df9cea9679e34528fec6cfbae6f34fd |
|
BLAKE2b-256 | d3650afcc6eeeffe6f7920e584543cdf8c8aec61b70372eb0f818b73b9894885 |