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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
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.
