from openpyxl import workbook
from openpyxl.reader.excel import load_workbook
from openpyxl.styles import PatternFill
import time
# 匹配村
def data_compare(excelfile,find_cun_col,find_country_col,befind_cun_col,befind_country_col,befind_code_col,result_cun_col,result_country_col,result_code_col,result_state_col):
try:
excel = load_workbook(excelfile,read_only=False)
sheet1 = excel["Sheet1"]
sheet2 = excel["Sheet2"]
rowmax1 = sheet1.max_row#
rowmax2 = sheet2.max_row#
fill = PatternFill(start_color='FFFF00',
end_color='FFFF00',
fill_type='solid')
for item in range(2,rowmax1+1): #
cun_old = sheet1.cell(row=item,column=find_cun_col).value # 获取村数据
cun_old_pre_two_char = cun_old[:2]
print("正在比对sheet1的第"+str(item)+"行")
# find_result = []
item2 = 2
while item2<rowmax2+1:
cun_new = sheet2.cell(row=item2,column=befind_cun_col).value
cuntry_old = sheet1.cell(row=item, column=find_country_col).value
country_new = sheet2.cell(row=item2, column=befind_country_col).value
cun_new_pre_two_char = cun_new[:2]
find_cun_state = cun_compare(cun_old,cun_new) # 对村的名称进行匹配
if find_cun_state == 1: #如果村名匹配成功,则继续匹配乡镇街道是否一样
find_cuntry_state = cuntry_compare(cuntry_old,country_new)
if find_cuntry_state == 1:
# find_result.append(cun_new) #把找到村名结果存在列表中
# find_result.append(country_new) # #把找到街道乡镇名结果存在列表中
sheet1.cell(row=item, column=result_cun_col).value = sheet2.cell(row=item2,
column=befind_cun_col).value # 存储找到的街道
sheet1.cell(row=item, column=result_country_col).value = sheet2.cell(row=item2,
column=befind_country_col).value # 存储找到的code
sheet1.cell(row=item, column=result_code_col).value = sheet2.cell(row=item2,
column=befind_code_col).value # 存储找到的code
sheet1.cell(row=item, column=result_state_col).value = "true" # 存储 找到的结果,true,false
sheet2.cell(row=item2,column=befind_code_col).fill=fill #设置找到的单元格的颜色
print("匹配成功:正在比对sheet1的第" + str(item) + "行")
item2 = rowmax2+1
elif cun_new_pre_two_char == cun_old_pre_two_char:
find_cuntry_state = cuntry_compare(cuntry_old, country_new)
if find_cuntry_state == 1:
# find_result.append(cun_new) #把找到村名结果存在列表中
# find_result.append(country_new) # #把找到街道乡镇名结果存在列表中
sheet1.cell(row=item, column=result_cun_col).value = sheet2.cell(row=item2,
column=befind_cun_col).value # 存储找到的街道
sheet1.cell(row=item, column=result_country_col).value = sheet2.cell(row=item2,
column=befind_country_col).value # 存储找到的code
sheet1.cell(row=item, column=result_code_col).value = sheet2.cell(row=item2,
column=befind_code_col).value # 存储找到的code
sheet1.cell(row=item, column=result_state_col).value = "true" # 存储 找到的结果,true,false
sheet2.cell(row=item2, column=befind_code_col).fill = fill # 设置找到的单元格的颜色
print("匹配成功:正在比对sheet1的第" + str(item) + "行")
item2 = rowmax2 + 1
else:
if item2 == rowmax2:
print("匹配失败:正在比对sheet1的第" + str(item) + "行")
sheet1.cell(row=item, column=result_state_col).value = "False" # 存储 找到的结果,true,false
item2 = item2 + 1
excel.save(excelfile)
except Exception as err:
print("正在比对sheet1的第" + str(item) + "行")
print("正在比对sheet2的第" + str(item2) + "行")
print("错误信息如下:"+err)
# 匹配村
def cun_compare(cun_old,cun_new):
if cun_old != cun_new: # 判断村的全称不匹配,则继续去掉村、社区、居委会等后缀继续匹配
cun_old = cun_op(cun_old)
cun_new = cun_op(cun_new)
if cun_old == cun_new:
compare_result=1 #匹配成功则返回1
else:
compare_result = 0 #匹配不成功则返回0
else:
compare_result=1
return compare_result
# 匹配乡镇
def cuntry_compare(cuntry_old,country_new):
if cuntry_old != country_new:
cuntry_old = country_op(cuntry_old)
country_new = country_op(country_new)
if cuntry_old == country_new:
compare_result = 1 # 匹配成功则返回1
# sheet1.cell(row=row_param,column=col_param).value = country_new
# sheet1.cell(row=row_param, column=col_param+1).value = "true"
else:
# sheet1.cell(row=row_param, column=col_param + 1).value = "false"
compare_result = 0 # 匹配失败则返回0
else:
# sheet1.cell(row=row_param, column=col_param + 1).value = "false"
compare_result = 1 # 匹配成功则返回1
return compare_result
# 把村名包括社区、居委会、村等后缀去掉
def cun_op(village_param):
if village_param[-3:] == "居委会":
village_param = village_param[:-3]
elif village_param[-2:] == "社区":
village_param = village_param[:-2]
elif village_param[-1:] == "村":
village_param = village_param[:-1]
return village_param
#print(village)
# 把乡镇、街道、工业园、办事处等后缀去掉
def country_op(cuntry_param):
if cuntry_param[-3:] == "工业园":
cuntry_param = cuntry_param[:-3]
elif cuntry_param[-3:] == "办事处":
cuntry_param = cuntry_param[:-2]
elif cuntry_param[-2:] == "街道":
cuntry_param = cuntry_param[:-2]
else:
cuntry_param = cuntry_param[:-1]
return cuntry_param
#print(cuntry)
if __name__ =="__main__":
print("——————————————————————匹配开始——————————————————————")
find_cun_col = int(6) #要找的村名在第几列
find_country_col = int(5) #要找的村名在第几列
befind_cun_col = int(4) # 被找村名在第几列
befind_country_col = int(3) # 被找的村名在第几列
befind_code_col = int(5) # 被找的村名在第几列
result_state_col = int(19) #查找结果标识保存在第几列
result_cun_col = int(20) #找到的村名结果保存在第几列
result_code_col = int(21) # 找到的code保存在第几列
result_country_col = int(22) #找到的街道名保存在第几列
# excelfile = "C:\\Users\\DELL\\Desktop\\衡阳-result"+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+".xlsx"
excelfile = "C:\\Users\\DELL\\Desktop\\衡阳--匹配1.xlsx"
try:
excel = load_workbook(excelfile,read_only=False)
sheet1 = excel["Sheet1"]
sheet2 = excel["Sheet2"]
rowmax1 = 5#sheet1.max_row
rowmax2 = 5#sheet2.max_row
data_compare(excelfile,find_cun_col,find_country_col,befind_cun_col,befind_country_col,befind_code_col,result_cun_col,result_country_col,result_code_col,result_state_col)
print("——————————————————————匹配结束——————————————————————")
except Exception as err:
print("错误信息如:"+err)
相关文章: