我不仅对python相当陌生,而且这是我在该论坛上的第一篇文章。我正在学习如何集成python和excel。我能够得到以下代码:
import numpy as np
import pandas as pd
import xlrd, xlwt
import xlsxwriter
path = "C:/Users/Python/data/"
data = np.arange(1, 101).reshape((10,10))
wb = xlsxwriter.Workbook(path + 'workbook.xlsx')
ws_1 = wb.add_sheet('first_sheet')
ws_2 = wb.add_sheet('second_sheet')
for c in range(data.shape[0]):
for r in range(data.shape[1]):
ws_1.write(r, c, data[c, r])
ws_2.write(r, c, data[c, r])
wb.close()
可以在Jupyter Notebook上并通过anaconda python shell工作,但是当我在Spyder中运行时,我在ipython控制台上收到以下错误消息:
runfile('C:/Users/Python/excel_integration1.py',wdir ='C:/ Users / Python')追溯(最近一次调用):
在运行文件('C:/Users/Python/excel_integration1.py',wdir ='C:/ Users / Python')中,文件“”,第1行
文件“ C:\ Users \ Anaconda2 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,行execfile(文件名,名称空间)中的行866
文件“ C:\ Users \ Anaconda2 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,第87行,位于execfile exec(编译(scripttext,filename,'exec'),glob,loc)
ws_1 = wb.add_sheet('first_sheet')中的文件“ C:/Users/Python/excel_integration1.py”,第7行
AttributeError:“工作簿”对象没有属性“ add_sheet”
我期待着您的帮助。
如xlsxwriter文档中所示,xlsxwriter中的方法名称为add_worksheet
。您正在使用add_sheet
。我怀疑您可能已经从xlwt或其他库中读取了示例,因为在xlwt中您可能已经
>>> import xlwt
>>> wb = xlwt.Workbook()
>>> wb.add_sheet("some name")
<xlwt.Worksheet.Worksheet object at 0x7f6633b466d8>
但是有了xlsxwriter
>>> import xlsxwriter
>>> wb = xlsxwriter.Workbook()
>>> wb.add_sheet("won't work")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Workbook' object has no attribute 'add_sheet'
>>> wb.add_worksheet("will work")
<xlsxwriter.worksheet.Worksheet object at 0x7f6632e70320>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句