import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
from tkinter import Menu
from tkinter.filedialog import askdirectory
import os
import win32com.client as win32
import re
class MyGui:
def __init__(self, init_window_name):
self.init_window_name = init_window_name
# 變數宣告
self.path = tk.StringVar()
self.tab_control = None
self.tab1 = None
self.tab2 = None
self.function1 = tk.IntVar() # 定義var1和var2整型變數用來存放選擇行為回傳值
self.function2 = tk.IntVar()
self.function3 = tk.IntVar()
self.c1 = None
self.c2 = None
self.c3 = None
self.e1_1 = None
self.e1_2 = None
self.e2 = None
self.e3_1 = None
self.e3_2 = None
self.b1 = None
# select path
def select_path(self):
path_ = askdirectory()
self.path.set(path_)
# 獲取目標目錄下所有excel檔案的路徑集合
def get_all_excel_file_paths(self):
root_path = self.path.get()
excel_file_paths_list = [] # 原始excel檔案地址串列
excel_file_paths_output_list = [] # 新建NEW檔案夾后excel檔案地址串列,輸出用
for root, dirs, names in os.walk(root_path):
for filename in names:
if filename.endswith('xlsx') or filename.endswith('xls'):
excel_file_path = root + '/{}'.format(filename)
excel_file_paths_list.append(excel_file_path) # 路徑和檔案名連接構成完整路徑
# 在根目錄新建NEW檔案夾,并將路徑中的\/全部替換為\\,處理后的檔案按原結構放入
excel_file_path_new = root + '/NEW/{}'.format(filename)
# excel_file_path.replace(root_path, root_path + '/NEW').replace(':/', r':\\')#.replace('\\', '\\\\').replace('/', '\\\\')
excel_file_paths_output_list.append(excel_file_path_new)
# for item in excel_file_paths_list:
# print(item)
# print("-------------------------------------")
return excel_file_paths_list, excel_file_paths_output_list
# 替換適用車型功能
def replacement_model(self):
# 打開excel前期準備
excel = win32.Dispatch("Excel.Application")
excel.DisplayAlerts = False # 關閉警告
excel.Visible = False # 程式不可見
pwd = os.getcwd()
# 選中的根目錄下新建NEW檔案夾
os.makedirs(self.path.get()+'/NEW')
# 呼叫函式獲取檔案原始地址和輸出地址
(excel_file_paths_list, excel_file_paths_output_list) = self.get_all_excel_file_paths()
# 確認復選框是否被選中
flag = self.function1.get()
if flag:
name_before_replacement = self.e1_1.get()
name_after_replacement = self.e1_2.get()
for input_path, output_path in zip(excel_file_paths_list, excel_file_paths_output_list):
print(input_path)
print(output_path)
my_excel = excel.Workbooks.open(input_path) # 打開一個已有的表格
my_sheet = my_excel.Worksheets(1) # 使第一個作業表處于作業狀態
for i in range(1, 80): # 查,改
for j in range(1, 80):
content = my_sheet.Cells(i, j).Value # 獲取當前單元格值
if content is not None:
content = str(content) # 所有內容轉換為字串格式
re.sub(name_before_replacement, name_after_replacement, content, flags=re.IGNORECASE)
print(content)
try:
my_sheet.Cells(i, j).Value = content
except Exception:
print('單元格寫保護')
print(output_path)
print(output_path.replace(':/', r':\\'))
my_excel.SaveAs(output_path.replace(':/', r':\\')) # 保存
excel.Quit() # 退出
# 設定視窗
def set_init_window(self):
self.init_window_name.title('Process Tools') # 視窗名
self.init_window_name.geometry('450x500') # 視窗大小
self.init_window_name["bg"] = "pink" # 視窗背景色
# self.init_window_name.attributes("-alpha", 0.9) # 視窗虛化
self.tab_control = ttk.Notebook(self.init_window_name) # Create Tab Control
self.tab1 = ttk.Frame(self.tab_control) # Create a tab
self.tab_control.add(self.tab1, text='Tab 1') # Add the tab
self.tab2 = ttk.Frame(self.tab_control) # Add a second tab
self.tab_control.add(self.tab2, text='Tab 2') # Make second tab visible
self.tab_control.pack(expand=1, fill="both") # Pack to make visible
tk.Label(self.tab1, text="選擇工藝檔案根目錄:").grid(row=0, column=0)
tk.Entry(self.tab1, textvariable=self.path).grid(row=0, column=1, ipadx=15)
tk.Button(self.tab1, text="路徑選擇", command=self.select_path).grid(row=0, column=2)
tk.Label(self.tab1).grid(row=1, column=0) # 空一行
# 選擇所需要的功能
tk.Label(self.tab1, text="選擇需要的功能:").grid(row=2, column=0, sticky="w")
tk.Label(self.tab1, text="需替換文本").grid(row=2, column=1)
tk.Label(self.tab1, text="替換后文本").grid(row=2, column=2)
self.c1 = tk.Checkbutton(self.tab1, text='替換適用車型', variable=self.function1, onvalue=https://bbs.csdn.net/topics/1, offvalue=0).grid(
row=3, column=0, sticky="w")
self.e1_1 = tk.Entry(self.tab1)
self.e1_1.grid(row=3, column=1)
self.e1_2 = tk.Entry(self.tab1)
self.e1_2.grid(row=3, column=2)
self.c2 = tk.Checkbutton(self.tab1, text='替換做成日期', variable=self.function2, onvalue=https://bbs.csdn.net/topics/1, offvalue=0).grid(
row=4, column=0, sticky="w")
self.e2 = tk.Entry(self.tab1)
self.e2.grid(row=4, column=2)
self.c3 = tk.Checkbutton(self.tab1, text='替換檔案名稱', variable=self.function3, onvalue=https://bbs.csdn.net/topics/1, offvalue=0).grid(
row=5, column=0, sticky="w")
self.e3_1 = tk.Entry(self.tab1)
self.e3_1.grid(row=5, column=1)
self.e3_2 = tk.Entry(self.tab1)
self.e3_2.grid(row=5, column=2)
# 進度條
tk.Label(self.tab1).grid(row=6, column=0) # 空一行
tk.Label(self.tab1, text="處理進度:").grid(row=7, column=0, sticky="E")
pb = ttk.Progressbar(self.tab1, length=300, value=https://bbs.csdn.net/topics/0, mode="indeterminate").grid(row=7, column=1, columnspan=2)
# 處理按鈕
self.b1 = tk.Button(self.tab1, text="開始處理", command=self.replacement_model).grid(row=8, columnspan=3)
def gui_start():
init_window = tk.Tk() # 實體化出一個父視窗
my_gui = MyGui(init_window)
my_gui.set_init_window() # 設定視窗默認屬性
init_window.mainloop() # 父視窗進入事件回圈
gui_start() # 開啟界面
【報警資訊】
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "D:/Workspaces/Python/GUI/processtools1.py", line 92, in replacement_model
my_excel.SaveAs(output_path.replace(':/', r':\\')) # 保存
File "<COMObject <unknown>>", line 7, in SaveAs
pywintypes.com_error: (-2147352567, '發生意外。', (0, 'Microsoft Excel', '無法訪問該檔案。請嘗試以下方法:\n\n? 確保指定的檔案夾存在。\n? 確保包含該檔案的檔案夾不是只讀的。\n? 確保檔案名和檔案夾路徑不包含以下任何字符:< > ? [ ] : | 或 *\n? 確保檔案名和檔案夾路徑不超過 218 個字符。', 'xlmain11.chm', 0, -2146827284), None)
uj5u.com熱心網友回復:
在界面中選擇了路徑,識別路徑下所有的excel檔案處理后在選擇的路徑下新建一個NEW檔案夾將處理好的檔案名字不變的存入到NEW檔案夾中,但是每次到保存時都報錯,搞了兩天沒搞出來uj5u.com熱心網友回復:
呼叫論他各位大神轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/235112.html
下一篇:大佬康我
