Last modified: Nov 14, 2025 By Alexander Williams

Python openpyxl Append Rows Bulk Update Guide

Working with Excel files is common in data processing. The openpyxl library makes it easy. This guide covers efficient row appending and bulk cell updates.

Introduction to openpyxl

Openpyxl is a Python library for Excel file manipulation. It supports both .xlsx and .xlsm file formats. You can read, write, and modify Excel files programmatically.

The library provides comprehensive Excel functionality. This includes formatting, charts, and formulas. Our focus is on data manipulation techniques.

Setting Up openpyxl

First, install openpyxl using pip. This is straightforward and quick.


pip install openpyxl

Import the library in your Python script. You're ready to start working with Excel files.


import openpyxl
from openpyxl import Workbook

Appending Rows to Excel Files

Appending rows is a common task. You might add new data to existing spreadsheets. Openpyxl provides several methods for this.

Using append() Method

The append() method adds rows to worksheets. It accepts lists, tuples, or ranges. Each element becomes a cell in the row.


# Create a new workbook
wb = Workbook()
ws = wb.active

# Data to append
data_rows = [
    ['Name', 'Age', 'City'],
    ['Alice', 30, 'New York'],
    ['Bob', 25, 'London'],
    ['Charlie', 35, 'Tokyo']
]

# Append each row
for row in data_rows:
    ws.append(row)

# Save the workbook
wb.save('people_data.xlsx')

This creates an Excel file with four rows. Each inner list becomes a worksheet row. The data is organized in columns.

Appending Single Rows

You can append individual rows as needed. This is useful for incremental data addition.


# Append a single row
ws.append(['Diana', 28, 'Paris'])

# Append with different data types
ws.append(['Eve', 32, 'Berlin', 'Engineer'])

Openpyxl handles different data types automatically. Numbers, strings, and dates are supported.

Bulk Cell Updates

Bulk updates modify multiple cells efficiently. This is faster than individual cell operations. It improves performance significantly.

Using iter_rows() for Bulk Updates

The iter_rows() method accesses cell ranges. You can iterate and update cells in batches.


# Update multiple cells at once
for row in ws.iter_rows(min_row=2, max_row=5, min_col=2, max_col=2):
    for cell in row:
        cell.value = cell.value + 1  # Increment age by 1

wb.save('updated_people.xlsx')

This increments all age values by one. The operation affects rows 2 through 5 in column B.

Bulk Value Assignment

You can assign values to multiple cells directly. This is the most efficient method for large updates.


# Bulk update using cell coordinates
ws['D1'] = 'Department'
ws['D2'] = 'Engineering'
ws['D3'] = 'Engineering'
ws['D4'] = 'Marketing'
ws['D5'] = 'Sales'

# Alternative method using range
departments = ['Engineering', 'Engineering', 'Marketing', 'Sales']
for i, dept in enumerate(departments, start=2):
    ws.cell(row=i, column=4).value = dept

Performance Optimization

Performance matters with large files. Bulk operations reduce execution time significantly. There are several optimization techniques.

For very large files, consider using openpyxl streaming write mode. This handles massive datasets efficiently.

Disable Calculations During Updates

Temporarily disable formula calculations. This speeds up bulk operations.


wb = Workbook()
wb.calculation = 'manual'

# Perform bulk updates
for row in range(2, 1001):
    for col in range(1, 11):
        ws.cell(row=row, column=col).value = f'Data{row}_{col}'

# Re-enable calculations
wb.calculation = 'auto'
wb.save('large_dataset.xlsx')

Working with Large Excel Files

Large files require special handling. Memory usage and performance become critical. Openpyxl provides solutions for this.

Learn more about handling large Excel files efficiently with Python. These techniques prevent memory issues.

Efficient Data Appending

Collect data before writing to disk. This reduces I/O operations and improves speed.


# Collect all data first
all_data = []
for i in range(1000):
    row_data = [f'Product_{i}', i * 10, f'Category_{i % 5}']
    all_data.append(row_data)

# Append all data at once
for row in all_data:
    ws.append(row)

wb.save('products.xlsx')

Advanced Techniques

Advanced methods provide more control. They handle complex scenarios effectively.

Appending from Dictionaries

Dictionary data can be appended directly. This is useful for structured data.


# Dictionary data
person_data = [
    {'Name': 'Frank', 'Age': 40, 'City': 'Sydney'},
    {'Name': 'Grace', 'Age': 29, 'City': 'Toronto'}
]

# Append dictionary values
for person in person_data:
    ws.append([person['Name'], person['Age'], person['City']])

Bulk Formatting with Cell Updates

Combine data updates with formatting. This creates professional-looking spreadsheets.

After updating cells, you might want to format Excel tables with Python openpyxl. This enhances readability and presentation.


from openpyxl.styles import Font, PatternFill

# Update cells and apply formatting
for row in ws.iter_rows(min_row=1, max_row=1):
    for cell in row:
        cell.font = Font(bold=True, color="FFFFFF")
        cell.fill = PatternFill(start_color="366092", end_color="366092", fill_type="solid")

Common Use Cases

These techniques solve real-world problems. Here are practical applications.

Data Export from Databases

Export query results to Excel. Append rows as you fetch data.


# Simulated database results
database_results = [
    ('2024-01-01', 'Product A', 1000),
    ('2024-01-02', 'Product B', 1500),
    ('2024-01-03', 'Product A', 1200)
]

# Append to Excel
headers = ['Date', 'Product', 'Sales']
ws.append(headers)

for record in database_results:
    ws.append(record)

wb.save('sales_report.xlsx')

Data Transformation

Modify existing Excel data. Bulk updates make transformations efficient.


# Convert temperature from Celsius to Fahrenheit
for row in ws.iter_rows(min_row=2, min_col=3, max_col=3):
    for cell in row:
        if cell.value is not None:
            celsius = float(cell.value)
            fahrenheit = (celsius * 9/5) + 32
            cell.value = round(fahrenheit, 2)

wb.save('converted_temperatures.xlsx')

Error Handling

Proper error handling ensures reliability. Catch exceptions during file operations.


try:
    wb = openpyxl.load_workbook('data.xlsx')
    ws = wb.active
    
    # Perform operations
    ws.append(['New', 'Data', 'Row'])
    wb.save('data.xlsx')
    
except FileNotFoundError:
    print("Excel file not found")
except PermissionError:
    print("Permission denied - file might be open")
except Exception as e:
    print(f"An error occurred: {e}")

Best Practices

Follow these guidelines for optimal results. They improve code quality and performance.

Always close workbooks after operations. Use context managers when possible. Validate data before appending.

Test with sample data first. Then scale to production datasets. Monitor memory usage with large files.

Conclusion

Openpyxl provides powerful tools for Excel manipulation. Appending rows and bulk cell updates are essential skills.

These techniques handle various data processing tasks. They work for small files and scale to large datasets.

Remember to optimize performance for large operations. Use the methods that best fit your specific needs.

With practice, you'll efficiently automate Excel tasks. This saves time and reduces manual errors significantly.