xlutils

2.0.0 · maintenance · verified Sun Apr 12

xlutils is a Python package offering various utilities for working with Excel files, specifically those compatible with `xlrd` and `xlwt`. It provides functionalities like copying `xlrd.Book` objects to `xlwt.Workbook` objects (xlutils.copy), displaying information, filtering, finding data margins, saving, and handling styles. The current version is 2.0.0. The library, along with its core dependencies `xlrd` and `xlwt`, is considered to be in maintenance mode or archived, with infrequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `xlutils.copy` to open an existing Excel file (in `.xls` format), make modifications to a cell, and save the changes to a new file. It first creates a dummy `.xls` file to ensure the example is runnable. `formatting_info=True` is crucial for attempting to preserve styles during the copy operation.

import xlrd
import xlwt
from xlutils.copy import copy

# Create a dummy .xls file first for demonstration
wb_initial = xlwt.Workbook()
ws_initial = wb_initial.add_sheet('Sheet1')
ws_initial.write(0, 0, 'Hello')
ws_initial.write(0, 1, 'World')
ws_initial.write(1, 0, 'Original Value')
wb_initial.save('example_input.xls')

# Open the existing workbook with xlrd
rb = xlrd.open_workbook('example_input.xls', formatting_info=True)

# Make a writable copy of the workbook using xlutils.copy
wb = copy(rb)

# Get the first sheet from the copied workbook
ws = wb.get_sheet(0)

# Write a new value to a cell (e.g., cell B2)
ws.write(1, 1, 'Modified Value')

# Save the modified workbook to a new .xls file
wb.save('example_output.xls')

print("Modified workbook saved as 'example_output.xls'")

# Clean up dummy file (optional)
import os
os.remove('example_input.xls')
os.remove('example_output.xls')

view raw JSON →