Skip to main content

Extract data from Excel files

Project description

PyPI - Python Version PyPI Code Style

XLExtract

An abstraction layer for quickly pulling data out of Microsoft Excel Spreadsheets.

The project is currently powered by the openpyxl library but you don't need to know anything about the underlying library and it could change in the future.

Installation

xlextract can be installed via poetry with: poetry add xlextract
or via pip with: pip install xlextract

What does it do?

xlextract searches a spreadsheet for a keyword you provide and extracts nearby data.
It provides three operations:

  1. Right Lookup: Extract cell value to the immediate right of the keyword
  2. Left Lookup: Extract cell value to the immediate left of the keyword
  3. Bottom Lookup: Extract cell value immediateley below the keyword
  4. Table Lookup: Extract an entire Excel table of data adjacent to the keyword. The table is modeled as a list of dictionaries where each list item represents a row in the table and the list item is a dictionary (key/value pairs) where each item in the dictionary represents a column of the row and where the key is the column header. An example will be provided below.

How do I use it in my project?

You need four bits of information to use xlextract:

  1. The name of the Excel file
  2. The name of the sheet in the Excel file
  3. The keyword you want to search for
  4. The type of lookup you want to do (Right, Left, or Table)

The project provides a class named XLExtract that requires the first 3 inputs above as strings.
The lookup is done via one of four class methods:

  1. RLookup() (Right Lookup)
  2. LLookup() (Left Lookup)
  3. BLookup() (Bottom Lookup)
  4. TLookup() (Table Lookup)

Here is an example of how to import the library and create a sheet object ready for lookup:

import json # This is just to print our example table with formatting

from xlextract import XLExtract

vrf_table = XLExtract("design_document.xlsx", "Tenants", "VRF NAME")

Table Lookup Example

Assuming we have an Excel spreadsheet with a "Tenants" tab that contains a table that looks like this:
Sample Excel Table If we then do this in our Python code:

vrf_table.TLookup()

print(json.dumps(vrf_table.value, indent = 4))

We would get the following output:

[
    {
        "TENANT": "Prod-tn",
        "VRF NAME": "Prod-VRF",
        "ENABLE PREFERRED GROUP": "enabled"
    },
    {
        "TENANT": "Prod-tn",
        "VRF NAME": "Dev-VRF",
        "ENABLE PREFERRED GROUP": "enabled"
    }
]

How does it do the table lookup?

The table lookup assumes the keyword you provide resides in the table header.
It first searches cells left of the keyword, then right of the keyword to establish table width.
When it encounters the first empty cell it assumes the edge of the table.
Then it moves down from the keyword cell, if data is present it captures the row.
When it encounters the first empty cell it assumes the bottom of the table, completing the extraction.

CAVEATS

The source Excel spreadsheet content should be planned ahead of time to account for these operational caveats:

  • Keywords on a given sheet must be unique
  • Keywords for a table should be one of the column headers
  • All cells in a table header should be populated for the table lookup to determine the correct table width
  • All data cells in the keyword column should be populated for the table lookup to determine the correct table height. Because of this you should not use columns with optional data for the keyword, or if you must, populate all the cells in the column with something ("N/A" or "empty" if a cell has no value for example).

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

xlextract-0.2.3.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

xlextract-0.2.3-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file xlextract-0.2.3.tar.gz.

File metadata

  • Download URL: xlextract-0.2.3.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.9 Linux/6.6.87.2-microsoft-standard-WSL2

File hashes

Hashes for xlextract-0.2.3.tar.gz
Algorithm Hash digest
SHA256 3c874f709dcd7986de53522e1254c7ae22e6b3e2305b1b24eab087b20b908631
MD5 76cd974b87de9f7dbcb666d8627957c6
BLAKE2b-256 4d8d5a14c9bd56bf470950e544f47fb967e40b376f7a5c8392eb7564dc298095

See more details on using hashes here.

File details

Details for the file xlextract-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: xlextract-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.9 Linux/6.6.87.2-microsoft-standard-WSL2

File hashes

Hashes for xlextract-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4b27921b84c433fcbe5ceaea1cc46926e28653bb0a8b9a6c022fb0b373bfd068
MD5 19c752e107b060cd3904821dd6dfee8a
BLAKE2b-256 7e311fd833f920684d42b3680e2ad6c60c5c7ded7bf8a6b320fd53b936233359

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