Guest User

Python file

a guest
Mar 26th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. """Reads, writes, analyses data present in an excel file (.xls or .xlsx)
  2.  
  3. Usage:
  4.    python xlanalyser.py <filename><filepath = optional>
  5.        filename:   Name of the excel document you want to open
  6.        filepath:   Location of the excel document
  7. """
  8.  
  9. import sys,xlrd,xlwt
  10.  
  11. class XlAnalyser():
  12.     """Reads, writes, analyses data present in an excel file"""
  13.     def __init__(self,filename,filepath = ''):
  14.         """Initialises data of the class
  15.  
  16.        Args:
  17.            filename:   Name of the excel document you want to open
  18.            filepath:   Location of the excel document
  19.        """
  20.         self._filename =  filename
  21.         if filepath != '':
  22.             self._iworkbook = xlrd.open_workbook(filepath + '/' + filename)
  23.         else:
  24.             self._iworkbook = xlrd.open_workbook(filename)
  25.         self._isheet = self._iworkbook.sheet_by_index(0)
  26.         self._oworkbook = xlwt.Workbook()
  27.         self._osheet = self._oworkbook.add_sheet(filename)
  28.         self._fieldindex = 0
  29.  
  30.  
  31.     def print_sheet(self):
  32.         """Prints the sheet in a tabular format
  33.        """
  34.         for r in range(self._isheet.nrows):
  35.             for c in range(self._isheet.ncols):
  36.                 print(self._isheet.cell_value(r,c), end = '\t')
  37.             print('')
  38.  
  39.  
  40.     def return_sheet(self):
  41.         """Returns the sheet in a list of lists
  42.        """
  43.         dataTable = []
  44.         for r in range(self._isheet.nrows):
  45.             datalist = []
  46.             for c in range(self._isheet.ncols):
  47.                 datalist.append(self._isheet.cell_value(r,c))
  48.             dataTable.append(datalist)
  49.         return dataTable
  50.  
  51.  
  52.     def copy_sheet(self,new_filename):
  53.         """Makes a copy of sheet removing the styles prensent in the sheet
  54.        """
  55.         for r in range(self._isheet.nrows):
  56.             for c in range(self._isheet.ncols):
  57.                 self._osheet.write(r,c,self._isheet.cell_value(r,c))
  58.         self._oworkbook.save(new_filename)
  59.  
  60.  
  61.     def find_field_index(self,fieldname):
  62.         """Finds the column number of the fieldname passed
  63.  
  64.        Returns:
  65.            The column number of the fieldname passed
  66.        """
  67.         for c in range(self._isheet.ncols):
  68.             if(fieldname.lower() == self._isheet.cell_value(0,c).lower()):
  69.                 self._fieldindex = c
  70.         return self._fieldindex
  71.  
  72.  
  73.     def calc_perct(self, fieldname, criteria, relation):
  74.         """Shows the percentage of records that meet the given criteria in the given fieldname
  75.  
  76.        Returns:
  77.            The percentage of records that meet the given criteria in the given fieldname
  78.        """
  79.         perct = 0.0
  80.         count = 0
  81.         fieldindex = self.find_field_index(self._filename)
  82.         for  r in range(1,self._isheet.nrows):
  83.             if relation == '=':
  84.                 if(float(self._isheet.cell_value(r,fieldindex)) == criteria):
  85.                     count += 1;
  86.             elif relation == '>=':
  87.                 if(float(self._isheet.cell_value(r,fieldindex)) >= criteria):
  88.                     count += 1;
  89.             elif relation == '<=':
  90.                 if(float(self._isheet.cell_value(r,fieldindex)) <= criteria):
  91.                     count += 1;
  92.             elif relation == '>':
  93.                 if(float(self._isheet.cell_value(r,fieldindex)) > criteria):
  94.                     count += 1;
  95.             elif relation == '<':
  96.                 if(float(self._isheet.cell_value(r,fieldindex)) < criteria):
  97.                     count += 1;
  98.             else:
  99.                 raise ValueError("{} relation not defined".format(relation))
  100.         perct = (count/(self._isheet.nrows-1)) * 100
  101.         return perct
Add Comment
Please, Sign In to add comment