EDIT: Using match’s suggestion below here’s my working code:
def buildReport():
global dirName #default dir or user selected dir
files=list(Path(dirName).glob('*.vsdx'))
print(files)
with tempfile.TemporaryDirectory(dir=defaultInitDir) as tempdir:
for index, file in enumerate(files):
with ZipFile(files[index]) as vsdx:
vsdx.extractall(tempdir)
for path in Path(tempdir).iterdir(): #Test to determine if tempdir was created
print(path)
With the following output:
[WindowsPath('C:/python/WPT-ReportBuilder/Working/vsdx/Test.vsdx'),
WindowsPath('C:/python/WPT-ReportBuilder/Working/vsdx/Test2.vsdx')]
C:\python\WPT-ReportBuilder\Working\tmpxsungorv\docProps
C:\python\WPT-ReportBuilder\Working\tmpxsungorv\visio
C:\python\WPT-ReportBuilder\Working\tmpxsungorv\[Content_Types].xml
C:\python\WPT-ReportBuilder\Working\tmpxsungorv\_rels
C:\python\WPT-ReportBuilder\Working\tmpxsungorv\docProps
C:\python\WPT-ReportBuilder\Working\tmpxsungorv\visio
C:\python\WPT-ReportBuilder\Working\tmpxsungorv\[Content_Types].xml
C:\python\WPT-ReportBuilder\Working\tmpxsungorv\_rels
I had to add the index to the for loop because I kept getting a TypeError
ORIGINAL:
I’m trying to store the paths of several .vsdx files located in a specific directory (that defaults to a directory relative to the script I’m running, or is user selectable through tkinter’s askdirectory command).
Once they’re stored in a list, I’m going to iterate through each file and do the following things:
- Unzip the file into a temporary folder
- Parse through some of the xml files included for data
- Build an excel report where each file has its own sheet with the data I’ve pulled from the .vsdx file
I’ve got a program that similarly grabs the xml data and have the GUI operational. I’m pretty confident that part will work, but I can’t get the files stored in a list properly to be able to loop through it.
Here’s what I’ve got so far:
def buildReport():
global dirName #default dir or user selected dir
files_Path=list(Path(dirName).glob('*.vsdx'))
print(files_Path)
with tempfile.TemporaryDirectory(dir=defaultInitDir) as tempdir:
files_Path[0].extractall(tempdir)
for path in Path(tempdir).iterdir(): #Test to determine if tempdir was created
print(path)
Here’s my output:
[WindowsPath('C:/python/WPT-ReportBuilder/Working/vsdx/Test.vsdx')]
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\python\WPT-ReportBuilder\Working\script1.py", line 47, in buildReport
files_Path[0].extractall(tempdir)
AttributeError: 'WindowsPath' object has no attribute 'extractall'
I’ve tried converting the path to a string, using os instead of pathlib, and several other things, but just can get it to work how I think it should.
I know it’s going to be something simple, but I’m very new to python and have been beating my head against this for a couple of days and can’t get anywhere. Please help and TYIA!
>Solution :
It looks like you probably want to use ZipFile to first load a zip archive, then call the extractall method on that instance. Something like:
from zipfile import ZipFile
with ZipFile(files_Path[0]) as zf:
zf.extractall(tempdir)