Python csv-improved xls code, pythoncsv-xls

Source: Internet
Author: User

Python csv-improved xls code, pythoncsv-xls

# Coding: UTF-8 # import the corresponding module import csv, xlwt, sys, OS, fnmatch, xlrdfrom xlutils. copy import copy # calculate the maximum value for the absolute value data in the xls file and list def max_excel (excel): P4 = xlrd. open_workbook (excel) # sheet0 = rb1.sheets () [1] wb1 = copy (P4) # The number of rows and columns can be obtained only after the sheet name is obtained on the sheet page, but cannot be written, the sheet obtained by subscript can be written, but the number of rows and columns cannot be obtained. sheet0 = rb1.sheet _ by_name ("Absolute") sheet3 = wb1.get _ sheet (2) nrows1 = sheet0.nrows ncols1 = sheet0.ncols for k in range (0, ncols1,): sheet3.write (0, k, u 'Blow point % d' % (k + 1 )) cols = sheet0.col _ values (k) cmax = max (cols [1:]) sheet3.write (1, k, cmax) wb1.save (excel) # Calculate the absolute value of data in the xls file def abs_excel (excel): # print "3" rb = xlrd. open_workbook (excel) # sheet = rb. sheets () [0] wb = copy (rb) sheet = rb. sheet_by_name ("data") # print sheet sheet2 = wb. get_sheet (1) nrows = sheet. nrows ncols = sheet. ncols for I in range (nrows): for j in range (ncols): w = sheet. cell (I, j ). value if (I = 0): sheet2.write (I, j, w) else: v = float (w) sheet2.write (I, j, abs (v) wb. save (excel) max_excel (excel) # save As xls file def ex_file (mycsvfile): csvfile = open (mycsvfile, "rb") # csvfile = open ("test.csv ", "rb") # create an excel file myexcel = xlwt. workbook () # create a sheet page mysheet1 = myexcel. add_sheet ("data") mysheet2 = myexcel. add_sheet ("Absolute") mysheet3 = myexcel. add_sheet ("MAX") # obtain the csv file name portion = OS. path. splitext (mycsvfile) # Read the file information in csv reader = csv. reader (csvfile, dialect = 'excel ') l = 0 # obtain the information of a single row in a loop for line in reader: r = 0 # obtain the information of a single unit in a dual loop for I in line: # Write the excel table mysheet1.write (l, r, I) r + = 1 l + = 1 myexcel in a double loop. save (portion [0] + ". xls ") excel = portion [0] + ". xls "# print portion [0] + ". xls "abs_excel (excel) def iterfindfiles (path, fnexp): for root, dirs, files in OS. walk (path): for filename in fnmatch. filter (files, fnexp): yield OS. path. join (root, filename) # batch processing if _ name __= = "_ main _": mypath = raw_input ("Please enter a path :") myfnexp = '*. csv '# print 1 for filename in iterfindfiles (mypath, myfnexp): # print filename ex_file (filename) # abs_excel (filename) # max_excel (filename) raw_input ('Please enter to exit ')

 

Finally, you can use the python pyinstaller -- console -- onefile absolute path \ py file. py command to generate the corresponding exe file.

 

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.