**
**
20万条公司信息,每家公司对应四个行业细分字段,现需要对各个公司的四个细分行业数据进行整合,去除重复项,保留和合并不同值
**
**
读写excel三方包:xlwings
注意:xlwings无法处理空值,需要预先在excel中填充空值,后续可使用excel替换函数删除
去重整合:set
import xlwings as xw
def open_sheet(filepath, sheet):
Excel = xw.App(visible=False,add_book=False)
Excel.display_alerts=False
Excel.screen_updating=False
ExcelFile = Excel.books.open(filepath)
global Excel, ExcelFile, Excel_sheet
#局部变量转化为全局变量
Excel_sheet = ExcelFile.sheets[sheet]
def close_sheet():
ExcelFile.save()
ExcelFile.close()
Excel.quit()
open_sheet(r'C:\Users\viola\Desktop\Sangfor\任务\4.老客户\demo(行业合并).xlsx','Sheet1')
value1 = Excel_sheet.range('A2').expand('down').value
value2 = Excel_sheet.range('B2').expand('down').value
value3 = Excel_sheet.range('C2').expand('down').value
new_value = []
#这四个是全局变量
for i in range(len(value1)):
a = {value1[i], value2[i], value3[i]}
new_value.append(str(a))
Excel_sheet.range('E2').options(transpose=True).value = new_value
close_sheet()