项目中要在excel要跨工作簿根据一列数据获取另一列的数据,而excel本身的函数vlookup一直不太好用,只能用程序进行处理了,最近刚接触了python,灵机一动使用Python进行处理,先将json格式处理成csv格式,再保存为excel,由于工作日报中要根据之前的json数据进行统计,数据行较大,人工进行工作量较大,然后使用python根据excel的内容进行处理。
import xlrd
import xlwt
from xlutils.copy import copy
import openpyxl
sheetName=‘20170716‘
dailyName=‘工作日报‘
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.nrows
dailyCols=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) # 某一行数据
#print(‘1‘+rowValues[0])
#print(‘2‘+dailyRowValues[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(‘无匹配保单数据‘)>-1:
sheet[‘I‘ + str(i + 1)] = ‘是‘
else:
if rowValues[2].find(‘无符合查询条件的保单数据‘)>-1:
sheet[‘I‘ + str(i + 1)] = ‘是‘
# print(item)
wb.save(dailyName+‘.xlsx‘)
原文:http://www.cnblogs.com/dzlishen/p/7192257.html