#coding: UTF-8
importarcpyimportosimporttypesimportstringimportshutilimportsysimportreimportopenpyxl#ws是一个xls的工作表#mode是替换模型,1完全替换,2模糊替换,3 追加替换
defreplacexls(ws,mode, text, replaceText):
rows=ws.max_row
cols=ws.max_column
changeCells=0if (mode <0):return -1
elif (mode>3):return -1
for row in range(1, rows + 1):for col in range(1, cols + 1):try:
content= ws.cell(row=row, column=col).valueif (content !=None):#mode1: fullmatch replacement
if (mode == 1):if (content ==text):
ws.cell(row=row, column=col).value =replaceText
changeCells+= 1
#mode2: partial replacement
elif (mode == 2):if (type(content) ==str):
ws.cell(row=row, column=col).value = content.replace(text, replaceText, 1)
changeCells+= 1
#mode3: partialmatch and filling
elif (mode == 3):if (type(content) ==str):
ws.cell(row=row, column=col).value = content.replace(text, text + replaceText, 1)
changeCells+= 1
exceptException as e:print(traceback.format_exc())returnchangeCellsdefchangeData(file, mode, text, replaceText):#load the file(*.xlsx)
wb =openpyxl.load_workbook(file)#! deal with one sheet
ws=wb.worksheets[0]
replacexls(ws, mode, text, replaceText)definitProgress(hint,num):
arcpy.SetProgressor(