I have a project where I need to unzip files containing a .xls
file and then run a Python script to convert the file to .xlsx
. I need to do this by executing a Windows Batch file through the Windows Task Scheduler. I found some code on Superuser to unzip the file, but it does not loop and you have to explicitly type the name of the file you want to unzip.
Here's the unzip code:
@echo off
setlocal
cd /d %~dp0
Call :UnZipFile "C:\Test Folder\" "C:\Test Folder\File1.zip"
exit /b
:UnZipFile <ExtractTo> <newzipfile>
IF "%~dp0"=="" GOTO Continue
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
Here's my Python code:
import win32com.client as win32
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--sourcefilepath')
parser.add_argument('--sourcefilename')
args = parser.parse_args()
fname = r"{sourcefilepath}{sourcefilename}".format(sourcefilepath=args.sourcefilepath,sourcefilename=args.sourcefilename)
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(fname)
wb.SaveAs(fname + "x", FileFormat = 56)
wb.Close()
excel.Application.Quit()
quit()
Any help will be great.