Issue
I want to open a file and then convert it from docx to zip. I am experiencing a blockade however with the selected file variable with the error of the title. "selected_file" in last line is highlighted as the bug in my IDE. Please know I am a beginner. My code is below:
import zipfile
import os
import tkinter as tk
from tkinter.filedialog import askopenfilename
tk.Tk().withdraw()
fn = askopenfilename(initialdir='/Desktop', title='Select a file', filetypes=(('docx file', '*.docx'), ('All files', '*.*')))
selected_file = open(fn)
with open(os.path.splitext(selected_file)[0] + '.zip')
Solution
Not quite sure what you want. Note that a .docx file is just a ZIP file containing (mostly) XML files. The code below will open a dialog on the current directory to select a DOCX file and list the files in the archive:
import zipfile
from tkinter.filedialog import askopenfilename
fn = askopenfilename(title='Select a file', filetypes=(('docx file', '*.docx'), ('All files', '*.*')))
with zipfile.ZipFile(fn) as z:
for file in z.namelist():
print(file)
Running this and selecting a DOCX file I had returns:
[Content_Types].xml
_rels/.rels
word/document.xml
word/_rels/document.xml.rels
word/theme/theme1.xml
word/settings.xml
word/styles.xml
word/webSettings.xml
word/fontTable.xml
docProps/core.xml
docProps/app.xml
Answered By - Mark Tolonen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.