Comprehensive Guide to Python Excel Libraries: xlrd, xlwt, xlutils, xlwings, openpyxl, XlsxWriter, win32com, and pandas
This article provides a detailed overview of multiple Python libraries for Excel manipulation—including xlrd, xlwt, xlutils, xlwings, openpyxl, XlsxWriter, win32com, and pandas—covering installation, basic operations, reading, writing, formatting, chart generation, and practical code examples for each tool.
This article introduces a wide range of Python libraries that enable reading, writing, and modifying Excel files, offering developers flexible tools for data processing and automation.
xlrd / xlwt : Used for reading and writing legacy .xls files. Installation via pip install xlrd xlwt . Example to read a sheet: import xlrd; wb = xlrd.open_workbook('file.xls'); sheet = wb.sheet_by_index(0); print(sheet.row_values(0)) . Example to write: import xlwt; wb = xlwt.Workbook(); ws = wb.add_sheet('Sheet1'); ws.write(0,0,'Hello'); wb.save('out.xls') .
xlutils : Extends xlrd/xlwt to copy and modify existing workbooks. Install with pip install xlutils . Sample usage: from xlutils.copy import copy; wb = xlrd.open_workbook('src.xls'); new_wb = copy(wb); new_wb.save('dest.xls') .
xlwings : Provides a high‑level interface to Excel, supporting reading, writing, formatting, and chart creation. Install with pip install xlwings . Basic workflow: import xlwings as xw; app = xw.App(visible=True); wb = xw.Book('file.xlsx'); sht = wb.sheets[0]; sht.range('A1').value = 'Data'; wb.save(); app.quit() . Includes examples for creating charts and manipulating cells.
openpyxl : Handles modern .xlsx files, allowing cell access, styling, and chart generation. Install via pip install openpyxl . Example to create a workbook: from openpyxl import Workbook; wb = Workbook(); ws = wb.active; ws['A1'] = 'Hello'; wb.save('demo.xlsx') . Also shows how to set fonts, alignments, merge cells, and create bar/3D charts.
XlsxWriter : Focuses on writing .xlsx files with rich formatting and chart support. Install with pip install XlsxWriter . Sample code: import xlsxwriter; wb = xlsxwriter.Workbook('report.xlsx'); ws = wb.add_worksheet(); ws.write('A1', 'Title', bold_format); chart = wb.add_chart({'type':'column'}); ws.insert_chart('E5', chart); wb.close() .
win32com : Allows automation of Excel through COM on Windows. Install using pip install pypiwin32 . Example to open and modify a workbook: import win32com.client as win32; app = win32.Dispatch('Excel.Application'); wb = app.Workbooks.Open(r'C:\path\file.xlsx'); sheet = wb.Worksheets('Sheet1'); sheet.Cells(1,2).Value = 'Updated'; wb.SaveAs(r'C:\path\new.xlsx'); app.Quit() .
pandas : Provides high‑level data structures for Excel I/O. Install with pip install pandas . Reading and writing: import pandas as pd; df = pd.read_excel('data.xlsx'); df.loc[4] = ['4','john','pandas']; df.to_excel('new.xlsx', index=False) . Demonstrates adding rows/columns and saving.
Each library section includes installation commands, core API usage, and practical code snippets, making this guide a one‑stop reference for Python developers working with Excel.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.