写了一段处理excel文件的代码,不使用def定义方法的时候可以正常运行,结果也是我想要的。但我定义方法,再次运行的时候,出现报错。
from __future__ import division
import xlrd
import xlwt
from xlutils.copy import copy
import time
import datetime
from datetime import date
import pandas as pd
import numpy as np
from pandas import DataFrame
import os,sys
from xlrd import open_workbook
from time import sleep, ctime
import threading
s='请输入文件:'
excelbook=raw_input(s.decode('utf-8').encode('gb2312')).strip()
workbook=xlrd.open_workbook(excelbook)
def readexcel():
# workbook = xlrd.open_workbook(r'D:\python\excel\fwqall.xls')
sheet = workbook.sheets()[0]
allrow = sheet.nrows
col6 = sheet.col_values(6) # 获取第6列内容
col7 = sheet.col_values(7)
def time():
readexcel()
wb = xlutils.copy(workbook)
ws = wb.get_sheet(0) # 获取第一个工作表
date_tmp = []
pbar = ProgressBar().start()
for rw in range(allrow):
if sheet.cell(rw, 6).ctype == 2: # 第六列转换为日期
date_value = xlrd.xldate_as_tuple(
sheet.cell_value(rw, 6), workbook.datemode)
date_tmp = date(*date_value[:3]).strftime('%Y,%m,%d')
# print date_tmp
for rs in range(allrow):
ws.write(rw, 6, date_tmp) # 写入
wb.save(excelbook)
…………
if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()
print 'all over %s'%ctime()
出现报错:
Exception in thread Thread-1:
Traceback (most recent call last):
File "c:\python27\lib\threading.py", line 801, in __bootstrap_inner
self.run()
File "c:\python27\lib\threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "D:\python\excel\src\1\1.1.py", line 30, in time
wb = copy(workbook)
NameError: global name 'copy' is not defined
请问这样该如何处理?
本人python刚入门,求指点,谢谢。
调用的是
xlutils.copy
但是你from xlutils.copy import copy
不一致吧应该是直接调用
copy