VBAからPythonを使ってパスワード付きzipを解凍する
2022/09/15
- VBAからPythonファイルを実行するの応用
- code-VBAサイド
unzip(pythonFolder, pythonFilename, zipFile, password)
で呼び出す- 埋め込み用pythonのあるフォルダ、実行用pythonファイル、zipファイル、パスワード
Function unzip(pythonFolder As String, pythonFilename As String, zipFile As String, password As String) Dim WSH, wExec, commandText As String Set WSH = CreateObject("WScript.Shell") commandText = "call """ & pythonFolder & "\python.exe""" commandText = commandText & " """ & pythonFilename & """" commandText = commandText & " """ & zipFile & """" commandText = commandText & " """ & password & """" Set wExec = WSH.Exec("%ComSpec% /c " & commandText) unzip = wExec.StdOut.ReadAll ' MsgBox wExec.StdErr.ReadAll End Function
- code-Pythonサイド
import pathlib import sys import zipfile args = sys.argv if len(args) != 3: print("Unzip Failed!\nThe file path and password are required.") sys.exit() filename = pathlib.Path(args[1]) password = args[2] unzip_path = filename.parent / filename.stem with zipfile.ZipFile(filename, "r") as zip: try: zip.extractall(path=unzip_path, pwd=password.encode("utf-8")) print("Successfully unzipped!") except RuntimeError as e: print(e)