Python文件管理器開發(fā)之文件遍歷與文檔預(yù)覽功能實現(xiàn)教程
項目概述
我們開發(fā)的文件管理器具備以下核心功能:
- 文件夾選擇與子目錄遍歷
- 文件列表顯示與管理
- Word和Excel文檔內(nèi)容預(yù)覽
- WPS Office集成打開功能
核心功能實現(xiàn)詳解
1. 文件夾遍歷功能
文件夾遍歷是文件管理器的基礎(chǔ)功能,我們采用了分層遍歷的設(shè)計思路。
1.1 主文件夾加載
def load_subfolders(self):
"""加載子文件夾到listbox1"""
self.listbox1.Clear()
self.listbox2.Clear()
self.preview_text.Clear()
if not self.current_folder or not os.path.exists(self.current_folder):
self.status_bar.SetStatusText("無效的文件夾路徑")
return
try:
subfolders = []
files = []
# 遍歷當(dāng)前文件夾中的所有項目
for item in os.listdir(self.current_folder):
item_path = os.path.join(self.current_folder, item)
if os.path.isdir(item_path):
subfolders.append(item)
elif os.path.isfile(item_path):
files.append(item)
# 排序并添加到列表
subfolders.sort()
for folder in subfolders:
self.listbox1.Append(folder)
# 處理特殊情況:沒有子文件夾但有文件
if not subfolders and files:
files.sort()
for file_name in files:
self.listbox2.Append(file_name)
self.status_bar.SetStatusText(f"當(dāng)前文件夾: 0 個子文件夾, {len(files)} 個文件")
else:
self.status_bar.SetStatusText(f"當(dāng)前文件夾: {len(subfolders)} 個子文件夾, {len(files)} 個文件")
except PermissionError:
wx.MessageBox("沒有權(quán)限訪問此文件夾", "錯誤", wx.OK | wx.ICON_ERROR)
self.status_bar.SetStatusText("權(quán)限錯誤")
except Exception as e:
wx.MessageBox(f"讀取文件夾時出錯:\n{str(e)}", "錯誤", wx.OK | wx.ICON_ERROR)
self.status_bar.SetStatusText(f"讀取錯誤: {str(e)}")
核心技術(shù)點解析:
- 路徑驗證:使用
os.path.exists()確保路徑有效 - 類型識別:通過
os.path.isdir()和os.path.isfile()區(qū)分文件夾和文件 - 異常處理:捕獲權(quán)限錯誤和其他異常,提供用戶友好的錯誤提示
- 智能顯示:當(dāng)沒有子文件夾時,直接顯示當(dāng)前目錄下的文件
1.2 文件列表加載
def load_files(self, folder_path):
"""加載文件到listbox2"""
self.listbox2.Clear()
if not os.path.exists(folder_path):
return
try:
files = []
for item in os.listdir(folder_path):
item_path = os.path.join(folder_path, item)
if os.path.isfile(item_path):
files.append(item)
# 排序文件列表
files.sort()
for file_name in files:
self.listbox2.Append(file_name)
self.status_bar.SetStatusText(f"找到 {len(files)} 個文件")
except PermissionError:
wx.MessageBox("沒有權(quán)限訪問此文件夾", "錯誤", wx.OK | wx.ICON_ERROR)
這里的關(guān)鍵是過濾機制,只獲取文件而忽略子目錄,確保文件列表的純凈性。
2. 文檔預(yù)覽功能實現(xiàn)
文檔預(yù)覽是本項目的亮點功能,支持多種文檔格式的內(nèi)容展示。
2.1 預(yù)覽分發(fā)器
def preview_file(self, file_path):
"""預(yù)覽文件內(nèi)容"""
self.preview_text.Clear()
if not os.path.exists(file_path):
self.preview_text.SetValue("文件不存在")
return
file_ext = Path(file_path).suffix.lower()
try:
if file_ext == '.docx':
self.preview_docx(file_path)
elif file_ext == '.doc':
self.preview_doc(file_path)
elif file_ext in ['.xlsx', '.xls']:
self.preview_excel(file_path)
else:
self.preview_text.SetValue(f"不支持預(yù)覽此文件類型: {file_ext}\n\n支持的文件類型:\n- Word文檔 (.docx, .doc)\n- Excel文件 (.xlsx, .xls)")
except Exception as e:
self.preview_text.SetValue(f"預(yù)覽文件時出錯:\n{str(e)}")
設(shè)計模式:采用了策略模式,根據(jù)文件擴展名選擇相應(yīng)的預(yù)覽策略。
2.2 Word文檔預(yù)覽實現(xiàn)
現(xiàn)代Word文檔(.docx)預(yù)覽
def preview_docx(self, file_path):
"""預(yù)覽Word文檔"""
try:
doc = Document(file_path)
content = []
content.append(f"文檔: {os.path.basename(file_path)}")
content.append("=" * 50)
# 讀取段落內(nèi)容
for i, paragraph in enumerate(doc.paragraphs):
if paragraph.text.strip():
content.append(f"段落 {i+1}: {paragraph.text}")
# 讀取表格內(nèi)容
if doc.tables:
content.append("\n" + "=" * 30 + " 表格內(nèi)容 " + "=" * 30)
for table_idx, table in enumerate(doc.tables):
content.append(f"\n表格 {table_idx + 1}:")
for row_idx, row in enumerate(table.rows):
row_data = []
for cell in row.cells:
row_data.append(cell.text.strip())
content.append(f" 行 {row_idx + 1}: {' | '.join(row_data)}")
self.preview_text.SetValue("\n".join(content))
self.status_bar.SetStatusText(f"已預(yù)覽Word文檔: {os.path.basename(file_path)}")
except Exception as e:
self.preview_text.SetValue(f"無法預(yù)覽Word文檔:\n{str(e)}\n\n請確保安裝了 python-docx 庫")
技術(shù)亮點:
- 使用
python-docx庫解析DOCX格式 - 分別處理段落和表格內(nèi)容
- 結(jié)構(gòu)化展示,便于閱讀
傳統(tǒng)Word文檔(.doc)預(yù)覽
def preview_doc(self, file_path):
"""預(yù)覽老式Word文檔(.doc)"""
try:
# 方法1: 使用 docx2txt
try:
import docx2txt
text_content = docx2txt.process(file_path)
if text_content and text_content.strip():
content = []
content.append(f"Word文檔: {os.path.basename(file_path)}")
content.append("=" * 50)
content.append("注意: 這是.doc格式文檔的文本內(nèi)容預(yù)覽")
content.append("=" * 50)
content.append(text_content)
self.preview_text.SetValue("\n".join(content))
self.status_bar.SetStatusText(f"已預(yù)覽Word文檔(.doc): {os.path.basename(file_path)}")
return
except ImportError:
pass
# 方法2: 使用 win32com
try:
import win32com.client as win32
word_app = win32.Dispatch('Word.Application')
word_app.Visible = False
try:
doc = word_app.Documents.Open(file_path)
text_content = doc.Content.Text
paragraph_count = doc.Paragraphs.Count
table_count = doc.Tables.Count
content = []
content.append(f"Word文檔: {os.path.basename(file_path)}")
content.append("=" * 50)
content.append(f"段落數(shù)量: {paragraph_count}")
content.append(f"表格數(shù)量: {table_count}")
content.append("=" * 50)
content.append("文檔內(nèi)容:")
content.append(text_content[:5000]) # 限制字符數(shù)
doc.Close()
self.preview_text.SetValue("\n".join(content))
return
finally:
word_app.Quit()
except ImportError:
pass
# 降級處理:顯示安裝指導(dǎo)
self.preview_text.SetValue(
f"無法預(yù)覽 .doc 格式文檔: {os.path.basename(file_path)}\n\n"
"要預(yù)覽 .doc 文件,請安裝以下任一依賴庫:\n\n"
"方法1 (推薦): pip install docx2txt\n"
"方法2: pip install mammoth\n"
"方法3 (Windows): pip install pywin32\n\n"
)
except Exception as e:
self.preview_text.SetValue(f"預(yù)覽.doc文檔時出錯:\n{str(e)}")
多重策略設(shè)計:
- docx2txt:輕量級文本提取
- mammoth:更好的格式保持
- win32com:利用系統(tǒng)Word程序,功能最全
- 降級處理:提供詳細(xì)的安裝指導(dǎo)
2.3 Excel文件預(yù)覽
def preview_excel(self, file_path):
"""預(yù)覽Excel文件"""
try:
excel_file = pd.ExcelFile(file_path)
content = []
content.append(f"Excel文件: {os.path.basename(file_path)}")
content.append("=" * 50)
content.append(f"工作表數(shù)量: {len(excel_file.sheet_names)}")
content.append(f"工作表名稱: {', '.join(excel_file.sheet_names)}")
content.append("")
# 預(yù)覽每個工作表的前幾行
for sheet_name in excel_file.sheet_names:
content.append("=" * 30 + f" {sheet_name} " + "=" * 30)
try:
df = pd.read_excel(file_path, sheet_name=sheet_name, nrows=10)
content.append(f"行數(shù): {len(df)}, 列數(shù): {len(df.columns)}")
content.append(f"列名: {', '.join(df.columns.astype(str))}")
content.append("")
content.append("前10行數(shù)據(jù):")
content.append(df.to_string(index=False, max_rows=10))
content.append("")
except Exception as e:
content.append(f"無法讀取工作表 {sheet_name}: {str(e)}")
self.preview_text.SetValue("\n".join(content))
except Exception as e:
self.preview_text.SetValue(f"無法預(yù)覽Excel文件:\n{str(e)}")
Excel處理特色:
- 使用pandas處理Excel文件
- 展示工作表結(jié)構(gòu)信息
- 限制數(shù)據(jù)行數(shù)避免界面卡頓
- 逐個工作表預(yù)覽
運行結(jié)果

到此這篇關(guān)于Python文件管理器開發(fā)之文件遍歷與文檔預(yù)覽功能實現(xiàn)教程的文章就介紹到這了,更多相關(guān)Python文件管理器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python輸出由1,2,3,4組成的互不相同且無重復(fù)的三位數(shù)
這篇文章主要介紹了Python輸出由1,2,3,4組成的互不相同且無重復(fù)的三位數(shù),分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
django框架基于queryset和雙下劃線的跨表查詢操作詳解
這篇文章主要介紹了django框架基于queryset和雙下劃線的跨表查詢操作,結(jié)合實例形式詳細(xì)分析了Django框架queryset和雙下劃線的跨表查詢相關(guān)實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下2019-12-12
Python中Collections模塊的Counter容器類使用教程
Counter是Python標(biāo)準(zhǔn)庫提供的一個非常有用的容器,可以用來對序列中出現(xiàn)的各個元素進(jìn)行計數(shù),下面就來一起看一下Python中Collections模塊的Counter容器類使用教程2016-05-05
python連接并簡單操作SQL?server數(shù)據(jù)庫詳細(xì)步驟
python作為一門十分火熱的編程語言,操作數(shù)據(jù)庫自然是必不可少的,下面這篇文章主要給大家介紹了關(guān)于python連接并簡單操作SQL?server數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下2023-06-06

