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.0.tar.gz (89.0 kB view details)

Uploaded Source

Built Distributions

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

Uploaded PyPy macOS 11.0+ ARM64

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

Uploaded PyPy macOS 11.0+ ARM64

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

Uploaded PyPy macOS 11.0+ ARM64

ez_excel_mgt-0.6.0-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.0-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.0-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.0.tar.gz.

File metadata

  • Download URL: ez_excel_mgt-0.6.0.tar.gz
  • Upload date:
  • Size: 89.0 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.0.tar.gz
Algorithm Hash digest
SHA256 4af95e3b180af007da69342fbbfa5cc73ec4814d90e891d09d9bcfccb0f3708e
MD5 31b681662960dd5d986d962d4d48ded4
BLAKE2b-256 1ca3d78cc029c524dbfb6d605c7fccc9bffad1746632bf5a7cf42229a29c29b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ez_excel_mgt-0.6.0-pp310-pypy310_pp73-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ded219051d55ca83790cf2a13f40e617b5e92f6a46975f7a9be9865b42ca05da
MD5 35c4e97b776e32d7be64414256f728e9
BLAKE2b-256 59643dcd2d599a102e94039372e1b1734e46e278c8c88d7e294d4109b9bd54b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ez_excel_mgt-0.6.0-pp39-pypy39_pp73-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 15e9485deb18511918800bef0644357060c96c3efc22a8f27ac9fe81f2c5925e
MD5 451f4fc80ea607ba44581769ecfacafe
BLAKE2b-256 7dc1b65046515e519e74b4cbd83ba6487cab8cac816b2fe5574717a2a67e9b4f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ez_excel_mgt-0.6.0-pp38-pypy38_pp73-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 deb363cada89f1d68c05ee177a5ad3783312d0ff8c319246e109543da523c077
MD5 72d93681776cb24c922670d73b837482
BLAKE2b-256 6bcb13df45c9173e38be3f70b70648ee4d8a65846891059c2c0849d6a40472a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ez_excel_mgt-0.6.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d76c550c72801dd2288088de1c069e18bcf45eca6d15427b0ad7b7a56adf9443
MD5 598b16c92de6b7c85585f2d88b3907e7
BLAKE2b-256 6113cd5a2209db7830b07706d5395c042798606082bd577e17977bcbede0de49

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ez_excel_mgt-0.6.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5c5467c22be75bdd32ed1decbc494e15cf9e369cfb11c0e85270a7ce5d521f6f
MD5 dc736a86e155c70566f5d63da6e7f6ec
BLAKE2b-256 633abe2334370d4a0e08d26c040b98860ee5ecd520f74a058f134695e04b9ddc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ez_excel_mgt-0.6.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 16c14e6cac1bf828fd283c3fb2683f8360333edff253b5a94c43b2d1fc4251c3
MD5 11a76827d454bd003f55db56288edb31
BLAKE2b-256 4baf40faf2b733d418e03ee7d4fc29b3f763ef2783878e36a49e0f1aaccf10f2

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