添加copy模块_复制Excel文件

 1 #--此脚本用于将目标表格写入新的表格--
 2 #!/usr/bin/python3
 3 # -*-coding:utf-8-*-
 4 #python读取Excel中单元格的内容返回的有5种类型ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
 5 #显示单元格属性方法sheet.cell(row,col).ctype 
 6 import xlrd
 7 import os
 8 from xlrd import xldate_as_tuple
 9 from datetime import datetime
10 from xlwt import *
11 class Copy_Excel_to_Excel:
12 #------------------读数据---------------------------------
13   fileName = "example.xlsx"
14   def Get_Current_Path(self):
15       path = os.getcwd()#os.getcwd() 方法用于返回当前工作目录,用作创建新excel文件的目录
16       return path
17   
18   def read_data(self):
19     bk = xlrd.open_workbook(self.fileName)
20     #shxrange = range(bk.nsheets)
21     try:
22       sh = bk.sheet_by_name("Sheet1")
23     except:
24       print("没有对应的sheet")
25     nrows = sh.nrows #获取行数
26     ncols = sh.ncols #获取列数
27     book = Workbook(encoding='utf-8')
28     sheet = book.add_sheet('Sheet1') #创建一个sheet
29     list_type = []
30     for i in range(0,nrows):
31       row_data = sh.row_values(i)
32       #获取第i行第3列数据
33       #sh.cell_value(i,3)
34       #---------写出文件到excel--------
35       #print ("-----正在写入 "+str(i)+" 行-----")
36       for j in range(0,ncols):
37           if sh.cell(i,j).ctype == 3:
38               date = xldate_as_tuple(sh.cell(i,j).value,0)
39               value = datetime(*date)
40               sheet.write(i,j,str(value))
41               #print(value)#测试打印时间数据
42           else:
43             sheet.write(i,j, label = sh.cell_value(i,j)) #向第i行第j列写入获取到的值
44           if i == nrows - 1:#获取最后一行数据的数据类型
45             list_type.append(sh.cell(i,j).ctype)
46     #print("-----数据一共打印:" + str(nrows-1) + "行-----")
47     #print("数据类型:")         
48     #print(list_type)
49     book.save("new.xls")
50   
51   def Run(self):
52     self.read_data()
53     path = self.Get_Current_Path()
54     print(path)
原文地址:https://www.cnblogs.com/dog-and-cat/p/11558811.html