Operations 4 min read

Convert Excel .xls to .xlsx with Python: A Step‑by‑Step Guide

This article explains how to use Python and the win32com library to automate the conversion of Excel .xls files (including .xlsm) to .xlsx format, covering essential libraries, FileFormat codes, and a complete code example with detailed explanations.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Convert Excel .xls to .xlsx with Python: A Step‑by‑Step Guide

Introduction

Python has become extremely popular for office automation, offering a more convenient alternative to VBA for batch processing.

Previously we covered converting Word documents; now we focus on handling Excel files.

Common Python libraries for Excel

xlrd : read‑only, supports both .xls and .xlsx.

xlwt : write‑only, supports only .xls.

openpyxl : read and write, supports only .xlsx.

When both .xls and .xlsx files need processing, it is advisable to convert them to a unified format first.

For the less‑supported .xlsm format, you can use a locally installed Microsoft Excel application via COM automation to convert the file.

Key points about SaveAs FileFormat

FileFormat=51 → .xlsx

FileFormat=56 → .xls

FileFormat=52 → .xlsm

FileFormat=23 → .csv

Code example

# Call Excel application
ExcelApp = win32com.client.Dispatch("Excel.Application")
ExcelApp.Visible = 0
ExcelApp.DisplayAlerts = 0
wb = ExcelApp.Workbooks.Open(path)
newpath = allpath + '\\converted.xlsx'
wb.SaveAs(newpath, FileFormat=51)
ExcelApp.Quit()

The full script includes imports, a function definition, and usage to convert a given .xls file to .xlsx, printing the current directory and demonstrating the conversion.

Resulting file is saved as shown in the screenshot.

Conversion result
Conversion result
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonfile conversionwin32com
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.