convert xlsx files to xls inside folders and subfolders in Excel VBA or Python -
i trying convert xlsx files xls using vba macro or python code.so far,i on converting files in single folder using below code:
sub processfiles() dim filename, pathname, savefilename string dim wb workbook dim initialdisplayalerts boolean pathname = "" filename = dir(pathname & "*.xlsx") initialdisplayalerts = application.displayalerts application.displayalerts = false while filename <> "" set wb = workbooks.open(filename:=pathname & filename, _ updatelinks:=false) wb.checkcompatibility = false savefilename = replace(filename, ".xlsx", ".xls") wb.saveas filename:=pathname & savefilename, _ fileformat:=xlexcel8, password:="", writerespassword:="", _ readonlyrecommended:=false, createbackup:=false wb.close savechanges:=false filename = dir() loop application.displayalerts = initialdisplayalerts end sub
i trying generalize subfolders inside given folder.
on larger note,i trying build macro following:
- run in batch mode parent folder constant.
- process code @ background , put converted files in respective folders.
for example, max main folder inside there may med ,det,vis,liv sub-folders.inside each subfolder,there thousands of xlsx files need converted , placed @ same location parent file stored.
please help.
thanks, maddy
you in python follows. take xlsx
files single folder , write them using same name in xls
format:
import win32com.client win32 import glob import os excel = win32.gencache.ensuredispatch('excel.application') excel_filename in glob.glob(r'c:\excel_files_folder\*.xlsx'): print excel_filename wb = excel.workbooks.open(excel_filename) wb.saveas(os.path.splitext(excel_filename)[0] + '.xls', fileformat=56, conflictresolution=2) excel.application.quit()
where 56
format number used, these listed on microsoft website.
to on whole directory structure, use os.walk
follows:
import win32com.client win32 import os excel = win32.gencache.ensuredispatch('excel.application') dirpath, dirnames, filenames in os.walk(r'c:\excel_files_folder'): filename in filenames: name, ext = os.path.splitext(filename) if ext == '.xlsx': wb = excel.workbooks.open(os.path.join(dirpath, filename)) wb.donotpromptforconvert = true wb.checkcompatibility = false excel.displayalerts = false wb.saveas(os.path.join(dirpath, name + '.xls'), fileformat=56, conflictresolution=2) excel.application.quit()
Comments
Post a Comment