#--此脚本用于将目标表格写入新的表格-- #!/usr/bin/python3 # -*-coding:utf-8-*- #python读取Excel中单元格的内容返回的有5种类型ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error #显示单元格属性方法sheet.cell(row,col).ctype import xlrd import os from xlrd import xldate_as_tuple from datetime import datetime from xlwt import * class Copy_Excel_to_Excel: #------------------读数据--------------------------------- fileName = "example.xlsx" def Get_Current_Path(self): path = os.getcwd()#os.getcwd() 方法用于返回当前工作目录,用作创建新excel文件的目录 return path def read_data(self): bk = xlrd.open_workbook(self.fileName) #shxrange = range(bk.nsheets) try: sh = bk.sheet_by_name("Sheet1") except: print("没有对应的sheet") nrows = sh.nrows #获取行数 ncols = sh.ncols #获取列数 book = Workbook(encoding='utf-8') sheet = book.add_sheet('Sheet1') #创建一个sheet list_type = [] for i in range(0,nrows): row_data = sh.row_values(i) #获取第i行第3列数据 #sh.cell_value(i,3) #---------写出文件到excel-------- #print ("-----正在写入 "+str(i)+" 行-----") 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)#测试打印时间数据 else: sheet.write(i,j, label = sh.cell_value(i,j)) #向第i行第j列写入获取到的值 if i == nrows - 1:#获取最后一行数据的数据类型 list_type.append(sh.cell(i,j).ctype) #print("-----数据一共打印:" + str(nrows-1) + "行-----") #print("数据类型:") #print(list_type) book.save("new.xls") def Run(self): self.read_data() path = self.Get_Current_Path() print(path)