Published on
247 words2 min read––– views

How to convert multiple xslb files to csv with python

How to convert multiple xslb files to csv with python

Manually converting xslb to csv is fine if you don't have too many xslb files and if you have xslb with smaller file size.

But, if you need to convert too many xslb files to csv, you can easily convert as many files you want using this simple python script.

Step 1:

  • Install python. Make sure you select 'Add Python 3.7 to PATH' during python installation.
  • Install pypiwin32 from command prompt using:
    python
    pip install pypiwin32

Step 2:

python
# import packages and set the xslb files directory path:
import os
import sys
import datetime
import win32com.client
import glob
excel = win32com.client.Dispatch("Excel.Application")
filePath="C:\\Users\\faisal\\Desktop\\xslbfiles\\"
# use glob to match the pattern 'xslb'
excel_files = glob.glob(filePath+'\*.xlsb')
for excel1 in excel_files:
print(excel1)
file = excel1.split('.')[0]+'.csv'
doc = excel.Workbooks.open(excel1)
doc.SaveAs(Filename=file,FileFormat=24)
doc.Close()
excel.Quit()

Save the above script as xslb_to_csv.py and run the script from command prompt using following line of code:

python
python C:\Users\faisal\Desktop\xslb_to_csv.py

or you can create a batch file with the above single line of code or run the script using any python ide.

After running the script you will have both xslb and csv files in same folder. You can sort by file types and separate csv files and xslb files.

The completed script for this how-to is documented on Github.