Copy_Excel_To_Excel # - This script is used to write a new table target table -

# - This script is used to write a new table target table - 
# / usr / bin / python3! 
# - * - Coding: UTF-8 - * - 
# Python read the contents of the cells in Excel has returned 5 types ctype: 0 empty, String. 1, Number 2, DATE. 3, Boolean. 4, error. 5 
# display method of cell properties sheet.cell (Row, COL) .ctype 
Import to xlrd
 Import OS
 from to xlrd Import xldate_as_tuple
 from datetime Import datetime
 from xlwt Import *
 class Copy_Excel_to_Excel:
 # ------------------ ---------------------- read data ----------- 
  fileName = " example.xlsx "
  DEF Get_Current_Path (Self): 
      path = os.getcwd () # os.getcwd () method returns the current working directory, the directory used to create new excel files 
      return path 
  
  DEF read_data (Self): 
    BK = xlrd.open_workbook (Self .FileName)
     # shxrange = Range (bk.nsheets) 
    the try : 
      SH = bk.sheet_by_name ( " Sheet1 " )
     the except :
       Print ( " no corresponding Sheet " ) 
    nrows = sh.nrows # Get the number of rows 
    ncols = sh.ncols # Gets the number of columns
    = the Workbook Book (encoding = ' UTF-. 8 ' ) 
    Sheet = book.add_sheet ( ' Sheet1 ' ) # Create a Sheet 
    list_type = []
     for I in Range (0, nrows): 
      ROW_DATA = sh.row_values (I)
       # Get the i-th row 3 data 
      # sh.cell_value (i, 3) 
      # --------- -------- write files to Excel 
      # Print ( "----- writing the "+ str (i) +" line ----- ") 
      for J in Range (0, ncols):
           IF sh.cell (I, J) == .ctype. 3 : 
              DATE= Xldate_as_tuple (sh.cell (I, J) .Value, 0) 
              value = datetime (* DATE) 
              sheet.write (I, J, STR (value)) 
              # Print (value) Test Print Time Data # 
          the else : 
            Sheet. write (i, j, label = sh.cell_value (i, j)) # writing to the i-th row j-th column value of the acquired 
          IF i == nrows -. 1: # Get the last line of data in the data type 
            list_type.append (sh.cell (I, J) .ctype)
     # Print ( "----- total Print data:" + str (nrows-1 ) + " line -----") 
    # Print ( "data type: ")          
    # Print (list_type) 
    book.save ( " new.xls " ) 
  
  DEF  the Run (Self):
    self.read_data ()
    path = self.Get_Current_Path()
    print(path)

 

Guess you like

Origin www.cnblogs.com/dog-and-cat/p/11613822.html