pylightxl

1.61 · active · verified Thu Apr 16

pylightxl is a lightweight, zero-dependency Python library designed for reading and writing Microsoft Excel files (.xlsx, .xlsm) and CSV files. It supports Python 2.7.18 and 3+, focusing on basic cell data manipulation without supporting complex features like formatting, graphs, or macros. The library is actively maintained, with its current version being 1.61, and releases occur regularly to address bugs and add features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple Excel file, write data including a formula, and then read data back from it using cell addresses, indices, and row iteration. It also shows how to access cell formulas.

import pylightxl as xl
import os

# Create a dummy Excel file for reading example
db_write = xl.Database()
db_write.add_ws(ws='Sheet1')
db_write.ws(ws='Sheet1').update_index(row=1, col=1, val='Header 1')
db_write.ws(ws='Sheet1').update_index(row=1, col=2, val='Header 2')
db_write.ws(ws='Sheet1').update_index(row=2, col=1, val=100)
db_write.ws(ws='Sheet1').update_index(row=2, col=2, val='=A2*2')

output_file = 'example_output.xlsx'
xl.writexl(db_write, output_file)
print(f"Created '{output_file}' for reading example.")

# Read from the Excel file
if os.path.exists(output_file):
    db_read = xl.readxl(fn=output_file)
    print(f"\nReading data from '{output_file}':")
    
    # Access data by address
    cell_a1_val = db_read.ws(ws='Sheet1').address(address='A1')
    print(f"Cell A1 (value): {cell_a1_val}")
    
    # Access formula by index
    cell_b2_formula = db_read.ws(ws='Sheet1').index(row=2, col=2, output='f')
    print(f"Cell B2 (formula): {cell_b2_formula}")
    
    # Iterate through rows
    print("\nAll data in Sheet1:")
    for row in db_read.ws(ws='Sheet1').rows:
        print(row)
else:
    print(f"Error: '{output_file}' not found.")

# Clean up the created file
if os.path.exists(output_file):
    os.remove(output_file)
    print(f"Cleaned up '{output_file}'.")

view raw JSON →