In the project, in Excel to get another column of data across the workbook according to one column of data, and Excel itself, the function vlookup has been not very good, can only be processed by the program, recently just touched Python, brainwave using python processing, First, the JSON format is processed into CSV format, and then saved as Excel, due to the daily work of the JSON data to be counted, the data row is large, manual work is large, and then use Python based on the content of Excel processing.
Import xlrdimport xlwtfrom xlutils.copy import copyimport openpyxlsheetname= ' 20170716 ' dailyname= ' work daily ' dailyData= Xlrd.open_workbook (dailyname+ '. xlsx ') dailysheets=dailydata.sheets () Dailysheetnames=dailydata.sheet_names () Index=dailysheetnames.index (sheetname) #print (index) #获取sheet索引 # for sheet in dailysheetnames:# print (sheet) Dailytable=dailysheets[index]dailyrows=dailytable.nrowsdailycols=dailytable.ncols#print (dailyRows) #日报文档data = Xlrd.open_workbook (sheetname+ '. xlsx ') Table = Data.sheets () [0]nrows = table.nrows #行数ncols = Table.ncols #列数wb = Openpyxl.load_workbook (dailyname+ '. xlsx ') sheet=wb.get_sheet_by_name (sheetname) #newWs =newwb.get_sheet (Index) # Oldwb.save (' test123.xlsx ') for I in Range (0,dailyrows): for J in range (0, nrows): Dailyrowvalues = Dailytable.row _values (i) Rowvalues = Table.row_values (j) # A row of data #print (' 1 ' +rowvalues[0]) #print (' 2 ' +dailyrowvalue S[4]) #print (dailyrowvalues[4]==rowvalues[0]) if DAILYROWVALUES[4]==ROWVALUES[0]: Print (i) # for item in rowvalues:sheet[' G ' +str (i+1)]=rowvalues[1] sheet[' H ' +str (i+1)] =ROWVALUES[2] If Rowvalues[2].find (' No Matching policy data ') >-1:sheet[' I ' + str (i + 1)] = ' is ' El Se:if Rowvalues[2].find (' No policy data in accordance with the query criteria ') >-1:sheet[' I ' + str (i + 1)] = ' Yes ' # Print (item) wb.save (dailyname+ '. xlsx ')
Read and write Excel using Python