Home > other >  Win32 couldn't save the excel file for help you a great god
Win32 couldn't save the excel file for help you a great god

Time:12-16

The import tkinter as tk
The from tkinter import TTK
The from tkinter import scrolledtext
The from tkinter import Menu
The from tkinter. Filedialog import askdirectory
The import OS
Import win32com. Client as win32
The import re


The class MyGui:
Def __init__ (self, init_window_name) :
Self. Init_window_name=init_window_name

# variable declaration
The self. The path=tk. StringVar ()
Self. Tab_control=None
Self. Tab1=None
The self. The tab2=None
Self. Once function1=tk. IntVar () # define var1 and var2 integer variables used to store choice behavior return value
Self. Function2=tk. IntVar ()
Self. Function3=tk. IntVar ()
The self. The c1=None
The self, c2=None
The self. The c3=None
Self. E1_1=None
Self. E1_2=None
Self. E2=None
Self. E3_1=None
Self. E3_2=None
Self. B1=None

# the select path
Def select_path (self) :
Path_=askdirectory ()
The self. The path. Set (path_)

# get target all excel file directory path set
Def get_all_excel_file_paths (self) :
Root_path=self. Path. The get ()
Excel_file_paths_list=[] # original excel file address list
Excel_file_paths_output_list=[] # after building the NEW folder address list of excel file, the output with
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 + '/{}'. The format (filename)
Excel_file_paths_list. Append (excel_file_path) # constitute the full path to the path and filename connection
# to build the NEW folder in the root directory, and the paths of \/all replacement for \ \, after processing the files according to the original structure in a
Excel_file_path_new=root + '/NEW/{}'. The format (filename)
# excel_file_path. Replace (root_path, root_path + '/NEW'). The replace (' :/'r' : \ \ ') #. Replace (' \ \ ', '\ \ \ \'). The replace ('/', '\ \ \ \')
Excel_file_paths_output_list. Append (excel_file_path_new)
# for the item in excel_file_paths_list:
# print (item)
# print (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
Return excel_file_paths_list, excel_file_paths_output_list

# replace vehicle range function
Def replacement_model (self) :
# to open the excel preparation
Excel=win32. Dispatch (" excel. Application ")
Excel. DisplayAlerts=False # close warning
Excel. Visible=False # program invisible
The PWD=OS. Getcwd ()

# selected to create NEW folder under the root directory of the
OS. Makedirs (self. Path. The get () + '/NEW')
# call functions get the original address and output file
(excel_file_paths_list excel_file_paths_output_list)=self. Get_all_excel_file_paths ()
# to confirm whether the check box is selected
Flag=self. Once function1. The get ()
The if flag:
Name_before_replacement=self. E1_1. The get ()
Name_after_replacement=self. E1_2. The 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) # open an existing table
My_sheet=my_excel. Worksheets (# 1) makes the first worksheet in the working state
For I in range (1, 80) : # check, change
For j in range (1, 80) :
The content=my_sheet. Cells (I, j). The Value # for current cell Value
If the content is not None:
The content=STR (content) # all content is converted into a string format
Re. Sub (name_before_replacement name_after_replacement, content, flags=re. The IGNORECASE)
Print (content)
Try:
My_sheet. Cells (I, j) Value=https://bbs.csdn.net/topics/content
Except the Exception:
Print (' cell write protect ')
Print (output_path)
: print (output_path. Replace ('/', r ': \ \'))
My_excel. SaveAs (output_path. Replace ('/' :, r ': \ \')) # save
# exit excel. The Quit ()

# Settings window
Def set_init_window (self) :
Self. Init_window_name. Title (' Process Tools') # window name
Self. Init_window_name. Geometry (' 450 x500 ') # window size
Self. Init_window_name [" bg "]="pink" # window background color
# self. Init_window_name. The attributes (0.9) - alpha, # window falsified

Self. Tab_control=TTK. Notebook (self. Init_window_name) # Create the Tab Control
Self. Tab1=TTK. Frame (self. Tab_control) # Create a TAB
Self. Tab_control. Add (self tab1, text='Tab 1) # add the Tab
The self. The 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, the fill="both") # pack to make visible

Tk. Label (self tab1, text="select process document root directory:"). The grid (row=0, the column=0)
Tk. Entry (self tab1, textvariable=self. The path). The grid (row=0, the column=1, ipadx=15)
Tk. The Button (self tab1, text="routing", the command=self. Select_path). The grid (row=0, the column=2)
Tk. Label (self. Tab1). The grid (row=1, the column=0) # empty row

# choose the required functionality
Tk. Label (self tab1, text="choose the required functions:"). The grid (row=2, the column=0, sticky="w")
Tk. Label (self tab1, text="to replace text"). The grid (row=2, the column=1)
Tk. Label (self tab1, text="replacement text"). The grid (row=2, the column=2)

Self. C1=tk. Checkbutton (self tab1, text='replace applicable model, variable=self. Once function1, onvalue=https://bbs.csdn.net/topics/1, offvalue=0). The grid (
The row=3, column=0, sticky="w")
Self. E1_1=tk. Entry (self tab1)
Self. E1_1. The grid (row=3, column=1)
Self. E1_2=tk. Entry (self tab1)
Self. E1_2. The grid (row=3, column=2)

Self. C2=tk. Checkbutton (self tab1, text='replacement into a date, variable=self. Function2, onvalue=https://bbs.csdn.net/topics/1, offvalue=0). The grid (
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related