Skip to main content

No project description provided

Project description

Easy Excel Management

A Rust-powered Python module for efficient Excel file manipulation.

  • Filling: Fill an existing sheet of an existing Excel file with data based on headers that are not necessarily in the first row of the sheet. Can handle Pandas, Polars and Dict of Lists. List of lists is also supported, provided the column names are provided too. Can handle overwriting existing data. Can skip nulls. Can handle strict matching of column names.

Installation

Install the module using pip or your favorite package manager:

   pip install ez-excel-mgt

Usage

Filling an existing sheet in an Excel file

Let's assume test.xls contains a sheet "Example" with with a few rows and columns. Let's assume the column names are contained in the third row.

Data can be provided in a Pandas, Polars, Dict of Lists or List of Lists (with column names specified in another argument).

Options

  • column: List of column names to use with a list of lists. This is optional if your DataFrame already has columns (Pandas, Polars or Dict).

  • header_row: Specifies the row containing the headers. Options:

    • "first": The first row is used as the header.
    • "last" (default): The last row is used as the header.
    • Integer > 0: The specific row number (1-based index) where the header is located.
  • overwrite: If True, existing data will be overwritten. Default is False.

  • skip_nulls: If True, null values in the data will be skipped. Default is False, which results in blanking cells for null values.

  • strict: If True, enforces strict matching: all column names must match exactly.

Example

from typing import List, Dict, Any
from pathlib import Path

import pandas as pd
import polars as pl
import openpyxl
import ez_excel_mgt as ezex

# Create a template.xlsx file with a sheet named "Sheet1" and a header row at row 3
def create_template_excel(tmp_path: Path, 
                          sheet_name: str, 
                          metadata: List[str], 
                          data: Dict[str, List[Any]]) -> int:
    # Create a new workbook and add a sheet
    workbook = openpyxl.Workbook()
    sheet = workbook.active
    sheet.title = sheet_name
    
    # Add metadata rows
    for i, row in enumerate(metadata):
        sheet[f"A{i+1}"] = row

    # Header row in row 3 (of the excel file)
    for j, (name, values) in enumerate(data.items()):
        sheet[f"{chr(65 + j)}{len(metadata)+1}"] = name
        for i, v in enumerate(values):
            sheet[f"{chr(65 + j)}{len(metadata)+i+1+1}"] = v

    # Write to the temporary file
    workbook.save(excel_path)
    
    return len(metadata) + 1


if __name__ == "__main__":
    sheet_name = "Example"
    excel_path = Path("example.xlsx")
    header_row = create_template_excel(excel_path,
                                       sheet_name, 
                                       ["A comment the first row", "Another in the second row"], 
                                       {"Name": ["Alice", "Bob", "Charlie"], 
                                        "Age": [25, 30, 35], 
                                        "Gender": ["F", "M", "M"],
                                        "City": ["New York", "London", "Paris"]})

    # Create a Pandas DataFrame
    pandas_df = pd.DataFrame({
        "Name": ["Anatole", "Erica", "Jules"],
        "Age": [85, 15, 95]
    })

    # Call the function to append the Pandas DataFrame to the existing sheet
    ezex.fill_sheet_with(pandas_df, str(excel_path), sheet_name, header_row=header_row)

    # Create a Polars DataFrame
    polars_df = pl.DataFrame({
        "Name": ["Philippe", "Paul"],
        "Age": [45, None],
        "Gender": ["M", "M"]
    })

    # Call the function to append the PolarsDataFrame to the existing sheet
    ezex.fill_sheet_with(polars_df, str(excel_path), sheet_name, header_row=header_row)

    # Create a dictionary with the data to be written to the Excel file
    dict_df = {
        "Name": ["Michel", "Amelie"],
        "Age": [35, 45],
        "Gender": ["M", "F"],
        "City": ["Paris", "London"]
    }

    # Read the Excel file into a Polars DataFrame
    ezex.fill_sheet_with(dict_df, str(excel_path), sheet_name, header_row=header_row)

    # Create a list of dictionaries with the data to be written to the Excel file
    # Column names must be provided separately
    columns = ["Age", "Gender", "City"]
    # skip_null allows to skip None values, which combined with overwrite enables filling gaps or replacing specific values like a mask
    list_df = [
        [None, None, None, None, None, None, None, 55, None, None],
        [None, None, None, "M", "F", "M", None, None, None, None],
        [None, None, None, "Brussels", "Madrid", "Berlin", "Lisbon", "Montreal", None, None]
    ]


    ezex.fill_sheet_with(list_df, str(excel_path), sheet_name, header_row=header_row, columns=columns, skip_null=True, overwrite=True)

    df = pl.read_excel(source=excel_path, sheet_name=sheet_name,
                       engine='xlsx2csv', engine_options={"skip_empty_lines": True, "skip_hidden_rows": False},
                       read_options={"skip_rows": 2, "has_header": True, "infer_schema_length": 0})
    print(df)

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

ez_excel_mgt-0.6.1.tar.gz (88.9 kB view details)

Uploaded Source

Built Distributions

ez_excel_mgt-0.6.1-pp310-pypy310_pp73-macosx_11_0_arm64.whl (5.3 MB view details)

Uploaded PyPy macOS 11.0+ ARM64

ez_excel_mgt-0.6.1-pp39-pypy39_pp73-macosx_11_0_arm64.whl (5.3 MB view details)

Uploaded PyPy macOS 11.0+ ARM64

ez_excel_mgt-0.6.1-pp38-pypy38_pp73-macosx_11_0_arm64.whl (5.3 MB view details)

Uploaded PyPy macOS 11.0+ ARM64

ez_excel_mgt-0.6.1-cp312-cp312-macosx_11_0_arm64.whl (5.3 MB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

ez_excel_mgt-0.6.1-cp311-cp311-macosx_11_0_arm64.whl (5.3 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

ez_excel_mgt-0.6.1-cp310-cp310-macosx_11_0_arm64.whl (5.3 MB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

File details

Details for the file ez_excel_mgt-0.6.1.tar.gz.

File metadata

  • Download URL: ez_excel_mgt-0.6.1.tar.gz
  • Upload date:
  • Size: 88.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.6

File hashes

Hashes for ez_excel_mgt-0.6.1.tar.gz
Algorithm Hash digest
SHA256 b1ac53906d4c87df74c3451b496740ae139e2f8fb00881d73f9784d7cd4f3f6d
MD5 d8c007a8265e27cc9fc65523aca0c57a
BLAKE2b-256 adde612f6a4b5b5b20a7e4878a6780649c15e03086e8e9aecc9b532df4863bbc

See more details on using hashes here.

File details

Details for the file ez_excel_mgt-0.6.1-pp310-pypy310_pp73-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ez_excel_mgt-0.6.1-pp310-pypy310_pp73-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fa32ee5a3380aa1607a3f98484d9bda0d6ba8c3a2c0243542034aeb2842c3636
MD5 6e0593e2b1882931b0b457a080b10113
BLAKE2b-256 281548a61cafdf9aa1eedd15e3cd0449d30d212e0f54bd4ff740c188f84ac5b6

See more details on using hashes here.

File details

Details for the file ez_excel_mgt-0.6.1-pp39-pypy39_pp73-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ez_excel_mgt-0.6.1-pp39-pypy39_pp73-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7b6e4db2b341d2f78ec82b66abe845723aa8e09d860e482019065f51f8fdff60
MD5 39aff7d9270dd0ee860d5116afe877d2
BLAKE2b-256 3cf45a4dd75ee3bc501803a8fe778621f09255cb608ffee772fc57be666d86a7

See more details on using hashes here.

File details

Details for the file ez_excel_mgt-0.6.1-pp38-pypy38_pp73-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ez_excel_mgt-0.6.1-pp38-pypy38_pp73-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ca9bccfb9874f092cdffeb99feabea92ee99cdfe600be311fef1d7798d9e1e90
MD5 b6aed6894fc2ce3f5e3f9afe726db079
BLAKE2b-256 4c2d9d102d19c09b2a243ec3a3a650d19a56937f623ce6c26d44b77e987e7a04

See more details on using hashes here.

File details

Details for the file ez_excel_mgt-0.6.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ez_excel_mgt-0.6.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 68cf9f0e6de779c746983f919788c414984e943eb4149894ea2c6567dbecadea
MD5 a8473747785893ab34eec5febf9b1696
BLAKE2b-256 e789b3b86e9be1fe760072035b98f1fd11a29128c2c4a02a60107f164f3a327a

See more details on using hashes here.

File details

Details for the file ez_excel_mgt-0.6.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ez_excel_mgt-0.6.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e47d2b1bb5007396c3597089b7714918e2d9d537ac9a17210e0168f72bef25e6
MD5 16d7d9018ed43605329c8a5c096368e7
BLAKE2b-256 1cca4b777cfbb16d479e79f906c6cc2040625141e8e709c05d5e08abdbf5f945

See more details on using hashes here.

File details

Details for the file ez_excel_mgt-0.6.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ez_excel_mgt-0.6.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 20678b80d61b4a1f51da5eac3e713f8ea149b3dac610e7f7e69a5fc112181061
MD5 cc6175edc948cc41072693c527020f46
BLAKE2b-256 aba96084881238bdd6eeda273394ab665f91430af8faa5a422011ae52163070c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page