Simple XLSX and CSV to dictionary converter
Project description
sheet2dict
A simple XLSX/CSV 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
# Import the library
from sheet2dict import Worksheet
# Create an object
ws = Worksheet()
# Convert
ws.xlsx_to_dict(path='inventory.xlsx')
# object.headers() returns first row with the data in a spreadsheet
print(ws.headers())
# 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.headers())
- 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.headers() returns first row with the data in a spreadsheet
print(ws.headers())
# object.sheet_items returns converted rows as dictionaries in the array
print(ws.sheet_items)
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
Hashes for sheet2dict-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb3054f36ef8cfb4530ef307132a9e8fff7072fa64d1e6bc698259b8cfb3951a |
|
MD5 | 122862f42a5a28830d32aafb6090c957 |
|
BLAKE2b-256 | 234bdba917103b3f64f96d8699bf7d6366f7f4cc6cd0f4f39600eb98b42e9bf5 |