使用Python + Tkinter + openpyxl實(shí)現(xiàn)Excel文本化轉(zhuǎn)換
在實(shí)際開發(fā)中,我們經(jīng)常需要將 Excel 中的所有數(shù)據(jù)轉(zhuǎn)換為字符串格式,尤其是處理包含 合并單元格 的復(fù)雜表格時(shí)。如果處理的數(shù)據(jù)量較大,程序運(yùn)行會(huì)比較慢,這時(shí)若能看到實(shí)時(shí)進(jìn)度條,就能更直觀地了解處理進(jìn)度。
本文將介紹一個(gè)使用 Python + Tkinter + openpyxl 開發(fā)的小工具,實(shí)現(xiàn)以下功能:
- 將 Excel 中所有單元格內(nèi)容轉(zhuǎn)換為文本
- 自動(dòng)處理合并單元格(取合并區(qū)左上角的值)
- Tkinter GUI 顯示實(shí)時(shí)進(jìn)度條
- 顯示處理行數(shù)及百分比
- 批量處理多個(gè) Sheet
非常適合 Excel 批處理、數(shù)據(jù)清洗、自動(dòng)化辦公開發(fā)者。
實(shí)現(xiàn)效果
程序運(yùn)行時(shí)會(huì)彈出一個(gè)包含進(jìn)度條的窗口:
- 每處理一行數(shù)據(jù)會(huì)實(shí)時(shí)更新進(jìn)度條
- 顯示百分比
- 顯示已處理的行數(shù)
- 整個(gè)文件轉(zhuǎn)換完成后,自動(dòng)保存回原文件
完整代碼(含詳細(xì)中文注釋)
import tkinter as tk
from tkinter import ttk
import openpyxl
def update_progress(progress_var, total_rows_processed, total_rows):
"""
更新進(jìn)度條百分比
progress_var: Tkinter 進(jìn)度變量
total_rows_processed: 已處理行數(shù)
total_rows: 總行數(shù)
"""
progress = total_rows_processed / total_rows * 100
progress_var.set(progress)
def process_sheet(sheet, progress_var, root, progress_label):
"""
處理單個(gè) Sheet,將所有單元格轉(zhuǎn)為字符串格式,
并處理合并單元格的值獲取。
sheet: 要處理的工作表
progress_var: Tkinter 進(jìn)度變量
root: Tkinter 主窗口(用于刷新界面)
progress_label: 進(jìn)度文本標(biāo)簽
"""
total_rows_processed = 0 # 已處理行數(shù)
total_rows = sheet.max_row # 當(dāng)前 sheet 的行數(shù)
# 遍歷所有行
for row in sheet.iter_rows():
# 遍歷行內(nèi)所有單元格
for cell in row:
# 判斷單元格是否屬于合并單元格
if cell.coordinate in sheet.merged_cells:
# 獲取該單元格所屬合并區(qū)域
merged_range = sheet.merged_cells[cell.coordinate]
# 獲取合并單元格左上角坐標(biāo)
top_left_cell = merged_range.min_row, merged_range.min_col
# 取左上角單元格的值
value = sheet.cell(*top_left_cell).value
else:
# 非合并單元格直接取值
value = cell.value
# 將單元格內(nèi)容強(qiáng)制轉(zhuǎn)換為字符串
cell.value = str(value)
# 該行處理完成
total_rows_processed += 1
# 更新進(jìn)度條顯示
update_progress(progress_var, total_rows_processed, total_rows)
progress_label.config(
text=f"Progress: {progress_var.get():.2f}%, Rows Processed: {total_rows_processed}/{total_rows}"
)
root.update_idletasks() # 強(qiáng)制刷新界面
if __name__ == "__main__":
file_path = "output_file.xlsx" # Excel 文件路徑
workbook = openpyxl.load_workbook(file_path)
# 創(chuàng)建 Tkinter GUI 窗口
root = tk.Tk()
root.title("Excel 文本化轉(zhuǎn)換(含進(jìn)度條)")
root.geometry("500x300")
# 進(jìn)度條變量
progress_var = tk.DoubleVar(value=0)
# 進(jìn)度條控件
progress_bar = ttk.Progressbar(root, mode="determinate",
variable=progress_var, maximum=100)
progress_bar.pack(pady=10)
# 顯示文本的標(biāo)簽
progress_label = tk.Label(root, text="Progress: 0.00%, Rows Processed: 0")
progress_label.pack(pady=5)
total_rows_processed = 0 # 總計(jì)處理行數(shù)
# 遍歷所有 Sheet 逐一處理
for sheet_name in workbook.sheetnames:
sheet = workbook[sheet_name]
total_rows = sheet.max_row
process_sheet(sheet, progress_var, root, progress_label)
total_rows_processed += total_rows
# 所有 Sheet 處理完畢,顯示 100%
update_progress(progress_var, total_rows_processed, total_rows_processed)
progress_label.config(
text=f"Progress: 100.00%, Rows Processed: {total_rows_processed}/{total_rows_processed}"
)
root.update_idletasks()
# 保存修改
workbook.save(file_path)
# 顯示完成界面
root.mainloop()
代碼講解
1. 處理合并單元格
openpyxl 中判斷單元格是否屬于合并區(qū)域可以直接這樣寫:
if cell.coordinate in sheet.merged_cells:
獲取合并區(qū)域左上角單元格:
merged_range = sheet.merged_cells[cell.coordinate] top_left_cell = merged_range.min_row, merged_range.min_col value = sheet.cell(*top_left_cell).value
2. Tkinter 實(shí)現(xiàn)進(jìn)度條與刷新界面
核心調(diào)用:
root.update_idletasks()
能保證界面實(shí)時(shí)更新,否則 Tkinter 會(huì)卡住直到任務(wù)完成才渲染 UI。
3. 將所有單元格轉(zhuǎn)換為字符串
非常簡(jiǎn)單:
cell.value = str(value)
總結(jié)
本文介紹了如何通過 Python、Tkinter 和 openpyxl:
- 批量處理 Excel 文件
- 自動(dòng)識(shí)別并處理合并單元格
- 轉(zhuǎn)換所有內(nèi)容為字符串
- 實(shí)時(shí)顯示處理進(jìn)度
適用于數(shù)據(jù)清洗、辦公自動(dòng)化、ETL、批處理等場(chǎng)景。
以上就是使用Python + Tkinter + openpyxl實(shí)現(xiàn)Excel文本化轉(zhuǎn)換的詳細(xì)內(nèi)容,更多關(guān)于Python Excel文本化轉(zhuǎn)換的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python 遞歸相關(guān)知識(shí)總結(jié)
這篇文章主要介紹了python 遞歸相關(guān)知識(shí)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03
Python將字符串常量轉(zhuǎn)化為變量方法總結(jié)
在本篇內(nèi)容里我們給大家整理了一篇關(guān)于Python將字符串常量轉(zhuǎn)化為變量方法的知識(shí)點(diǎn)總結(jié),有需要的朋友們學(xué)習(xí)下。2019-03-03
python實(shí)現(xiàn)查找excel里某一列重復(fù)數(shù)據(jù)并且剔除后打印的方法
這篇文章主要介紹了python實(shí)現(xiàn)查找excel里某一列重復(fù)數(shù)據(jù)并且剔除后打印的方法,涉及Python使用xlrd模塊操作Excel的相關(guān)技巧,需要的朋友可以參考下2015-05-05
Python在6個(gè)人工智能領(lǐng)域的實(shí)際應(yīng)用示例代碼
Python在人工智能(AI)領(lǐng)域得到了廣泛的應(yīng)用,從機(jī)器學(xué)習(xí)到深度學(xué)習(xí),從自然語(yǔ)言處理到計(jì)算機(jī)視覺,Python提供了豐富的庫(kù)和框架,使得開發(fā)者能夠快速實(shí)現(xiàn)各種AI應(yīng)用,本文將通過多個(gè)實(shí)際案例,展示Python在人工智能領(lǐng)域的強(qiáng)大功能和應(yīng)用前景2025-08-08

