No project description provided
Project description
Easy Excel Management
A Rust-powered Python module for efficient Excel file manipulation.
Installation
Install the module using pip
or your favorite package manager:
pip install ez-excel-mgt
Usage
Creating and Filling an Excel File
Create an Excel file (test.xlsx
) with a few rows and columns in a sheet named "Sheet1".
The sheet name must exactly match the name you'll use in the code. The last row in the sheet should contain the headers (column names) like "Name"
and "Age"
. Make sure the column names match exactly the column names in the DataFrame.
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 isFalse
. -
skip_nulls: If
True
, null values in the data will be skipped. Default isFalse
, 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
Built Distributions
Hashes for ez_excel_mgt-0.4.4-pp310-pypy310_pp73-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1293ecfe39bae79fb9fa11f7ad7a5fdd30f92087af14764a30da885573fba2a5 |
|
MD5 | f2d5e6123961ced216d791d77c92dc4c |
|
BLAKE2b-256 | b3406e536f7a83a8fbb506c88ee70fcd54f2cd6df842fadb50627193ad467102 |
Hashes for ez_excel_mgt-0.4.4-pp39-pypy39_pp73-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b985e2ff4044f327a56866d4f1f35a6e6b4a33037d6b3ebddd4f37a79af1400c |
|
MD5 | 0d4b56c3162aa8fc1138913611452749 |
|
BLAKE2b-256 | 6d1fa566d570a9ecb498b06a48eff516311aca004085ef29570ec9a002a5056b |
Hashes for ez_excel_mgt-0.4.4-pp38-pypy38_pp73-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c352133a23c25a75f01edbb670f41bfe2e0cf2c554d3bc7a71ae936c96cf840e |
|
MD5 | 83fcfee2e106c80c4a8f37485d8e7b9c |
|
BLAKE2b-256 | db35843c8d791cf8233aff08d90f7d7e7e13e374ba24b2bb9f1848a440752bef |
Hashes for ez_excel_mgt-0.4.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a8cbc790daa319d2ea95d9f1825da0199c3d360222b3cf90b97337b74171f1d |
|
MD5 | 412cb029c45c342a00abdd8a542b7b7e |
|
BLAKE2b-256 | d45b0e1c08b50f7ff1e512722ea9a75ea6c5afe64211a8c119f6e0e152f0a77c |
Hashes for ez_excel_mgt-0.4.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed43ee04465d02d011a62f74e116c1d77711b8da6609b1ad04f37d58a7e181e4 |
|
MD5 | b4a1125670f977ac4960b477eca18023 |
|
BLAKE2b-256 | 38430134df634237783be21089f10edecb4dade4c988a0c1ffed71013e6fa02c |
Hashes for ez_excel_mgt-0.4.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dacc67b501e9233a2ae9a55c2aceb25a14744deb7ccc913de4d17445d4c6b03f |
|
MD5 | 91265441fe9ccb84042e4e5a8b08432c |
|
BLAKE2b-256 | 1114a3205acdc8305612b20a780416cf8c52ace413d4e44ac2e58fba5fd8f6f3 |