Message from discussion
Script to convert from XLSX to XLS
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Received: by 10.100.135.11 with SMTP id i11mr358362and.27.1245083381639; Mon,
15 Jun 2009 09:29:41 -0700 (PDT)
Date: Mon, 15 Jun 2009 09:29:41 -0700 (PDT)
X-IP: 155.212.249.162
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.11)
Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729),gzip(gfe),gzip(gfe)
Message-ID: <0c868271-d6f4-4ae6-9546-16c7a8314183@d25g2000prn.googlegroups.com>
Subject: Script to convert from XLSX to XLS
From: Michael <selmo2...@gmail.com>
To: python-excel <python-excel@googlegroups.com>
Recently I needed to quickly convert XLSX workbooks to XLS workbooks
so I could then interact with them via xlrd. Here it is, hopefully it
will be useful to someone. :) Note that pywin32 is required to
interact with Excel 2007, so unfortunately this script will work only
on Windows with Excel 2007 installed.
This script is to be executed in the directory of XLSX workbooks
pending conversion.
import glob
import os
import time
import win32com.client
xlsx_files = glob.glob('*.xlsx')
if len(xlsx_files) == 0:
raise RuntimeError('No XLSX files to convert.')
xlApp = win32com.client.Dispatch('Excel.Application')
for file in xlsx_files:
xlWb = xlApp.Workbooks.Open(os.path.join(os.getcwd(), file))
xlWb.SaveAs(os.path.join(os.getcwd(), file.split('.xlsx')[0] +
'.xls'), FileFormat=1)
xlApp.Quit()
# Delete or comment out the following lines if you want to preserve
the
# original XLSX files.
time.sleep(2) # give Excel time to quit, otherwise files may be locked
for file in xlsx_files:
os.unlink(file)