Read and write Excel using Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.