SoFunction
Updated on 2025-05-15

10 Common Library Operation Guides for Efficiently Processing Excel Files in Python

I believe that many people in the workplace will feel that "Can't Excel handle it by itself? Why do you still need Python?"Well, to be honest, I felt so at the beginning. Until one day, when my boss asked me to combine hundreds of Excel data to do statistics, I could do the workload of the previous few days in Python in half a day. So mastering some of the small skills of Python automation can really help us a lot.

OK, without further ado, we start to get to the point. The following are 10 commonly used Python libraries and their operation methods. It is recommended to collect them and read them slowly.

1. Pandas — the most commonly used library

Pandas is a super killer when we work on Excel files, especially when it comes to data analysis. If you haven't used Pandas yet, first say ok, you'll likely fall in love with it in the future. Pandas can not only read and write to Excel, but also quickly clean and analyze data. It is simply a must-have tool for data scientists.

Common operations:

import pandas as pd

# Read Excel filedf = pd.read_excel('', sheet_name='Sheet1')

# Data cleaning: For example, delete NaN valuesdf = ()

# Write to Exceldf.to_excel('', index=False)

Pandas'sread_excelandto_excelThe method allows you to quickly read and write Excel, especially if the data is large, Pandas's efficiency in memory operations makes it a must-have tool.

2. Openpyxl — a right-hand assistant for manipulating Excel files

Openpyxl is another popular library that is mainly used to read and write to Excel.xlsxFile in format. Compared with Pandas, it pays more attention to the operation of cell content and can handle styles, formulas, etc.

Common operations:

from openpyxl import load_workbook

# Read the workbookwb = load_workbook('')

# Get the activity worksheetsheet = 

# Read cellprint(sheet['A1'].value)

# Modify cell contentsheet['A1'] = 'Hello, World!'

# Save the file('')

Tips:If you need to operate the format of cells, such as fonts, colors, and borders, Openpyxl will give you more flexibility, but its functions are slightly more complicated than Pandas. It is recommended to choose to use them for specific scenarios.

3. xlrd — Classic Read Library (but only for older versions)

Speaking of Excel processing, the name xlrd is almost the "tag" of older generation Python developers. It is most commonly used to read Excel files, however, with the popularity of the xlsx format, xlrd only supports reading the xls format after 2020.

Common operations:

import xlrd

# Open Excel filewb = xlrd.open_workbook('')

# Read the first worksheetsheet = wb.sheet_by_index(0)

# Get cell contentprint(sheet.cell_value(0, 0))

Notice:If you are dealing with newer .xlsx files, xlrd is not very suitable. You can consider usingopenpyxl

4. xlwt — a veteran player who writes Excel

If you need to write to an Excel file in xls format,xlwtIt's a good choice. It does not support the xlsx format, but it is very good for older xls files.

Common operations:

import xlwt

# Create a workbookwb = ()

# Create a worksheetsheet = wb.add_sheet('Sheet1')

# Write data(0, 0, 'Hello, xlwt!')

# Save the file('')

5. XlsxWriter — Write gracefully to Excel

If you want to do more formatting operations in Excel files (such as setting cell background colors, borders, etc.),XlsxWriterWill be your good friend. It can process xlsx files and can also do complex charts, formula embedding and other operations.

Common operations:

import xlsxwriter

# Create a workbookwb = ('')

# Create a worksheetsheet = wb.add_worksheet()

# Write data('A1', 'Hello, XlsxWriter!')

# Set the formatbold = wb.add_format({'bold': True})
('A2', 'Bold Text', bold)

# Save the file()

6. xlwings — Control Python with the power of Excel

This library directly connects Excel and Python, and operates through Excel's VBA interface, suitable for scenarios where stronger interactivity or control Excel workbooks are required. It is best for office automation, macro operations and chart generation.

Common operations:

import xlwings as xw

# Open Excel appapp = (visible=True)

# Create a workbookwb = ()

# Select a worksheetsheet = [0]

# Write data('A1').value = 'Hello, xlwings!'

# Save and close('')
()

7. pyexcel — easy to use

pyexcel is a simple and lightweight library that is especially suitable for processing tabular data and does not need to be as complicated as Pandas. It can read, write, modify Excel files, and directly convert data formats.

Common operations:

import pyexcel as pe

# Read Excel filerecords = pe.get_records(file_name="")

# Print contentfor record in records:
    print(record)

# Write to Excelpe.save_as(records=records, dest_file_name="")

8. et_xmlfile — Excel in XML format

The main function of this library is to process Excel files in XML format (for example, xlsx files are actually an XML-based file). It is an underlying operation library suitable for efficient and low-level operations on Excel.

Common operations:

from et_xmlfile import xmlfile

# Read Excel's XML formatwith xmlfile('') as f:
    ("<xml><data>Example</data></xml>")

9. odfpy — Process OpenOffice files

If your office software is OpenOffice or LibreOffice, odfpy can help you read and write .odt or .ods files. This is a relatively niche but practical tool for Python developers.

Common operations:

from  import OpenDocumentSpreadsheet
from  import P

# Create an ODF filedoc = OpenDocumentSpreadsheet()

# Add paragraphtext = P(text="Hello, ODF!")
(text)

# Save the file("")

10. pywin32 — Direct conversation with Excel

If you are a Windows user, pywin32 allows you to operate Excel applications directly. Through the COM interface, you can do almost any Excel operation, including opening and closing files, running macros, etc.

Common operations:

import 

# Start Excel applicationexcel = ("")
 = True

# Open the fileworkbook = (r'C:\path\to\your\')

# Get a worksheetsheet = (1)

# Modify the cell(1, 1).Value = 'Hello, pywin32!'

# Save and close()
()

Summarize

Each of these 10 libraries has its own unique advantages and usage scenarios. Choose the right tools according to your needs, and they will make your Excel operation twice the result with half the effort, saving time and effort. Of course, Python's power is more than that, and many other libraries can also help you solve various data processing problems.

This is the article about 10 common library operation guides for efficient processing of Excel files in Python. For more related content on Python processing of Excel, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!