|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如下所示,自定义一个函数:(函数功能是创建一个Excel,然后将Excel对象work_book作为返回值返回)
def create_work_book(file_path, file_name):
work_book_name = file_name + '.xls'
work_book_path = os.path.join(file_path, work_book_name)
print("work_book_path=%s" % work_book_path)
if os.path.exists(work_book_path):
return work_book_path
work_book = xlwt.Workbook(encoding='ascii')
# work_sheet = work_book.add_sheet('sheet')
work_book.save(work_book_path)
return work_book
我在其它地方调用该函数,再插入一张表及数据,如下所示:
work_book = create_work_book(root, current_dir_name)
work_sheet = work_book.add_sheet("123")
work_sheet.write(row, 0, behavior_time)
work_sheet.write(row, 1, draw_time)
运行的时候,在红色的地方报错了,错误信息说是字符串没有add_sheet()属性,这是为什么???它不是对象吗,怎么变成字符串了???
create a excel
work_book_path=C:\Users\Administrator\Desktop\SAVIC\2020-05-21-log\BasicContainer\BasicContainer.xls
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/SAVIC/python_script/main.py", line 55, in <module>
work_sheet = work_book.add_sheet("123")
AttributeError: 'str' object has no attribute 'add_sheet'
麻烦各位指点困惑,谢谢!!! |
|