我想打開一個檔案,然后將它從 docx 轉換為 zip。但是,我遇到了帶有標題錯誤的選定檔案變數的封鎖。最后一行中的“selected_file”突出顯示為我的 IDE 中的錯誤。請知道我是初學者。我的代碼如下:
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')
uj5u.com熱心網友回復:
不太確定你想要什么。需要注意的是.docx檔案是只含(大部分)的XML檔案的ZIP檔案。下面的代碼將在當前目錄上打開一個對話框,以選擇一個 DOCX 檔案并列出存檔中的檔案:
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)
運行它并選擇我回傳的 DOCX 檔案:
[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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/359982.html
