为了账号安全,请及时绑定邮箱和手机立即绑定

大家看看这段代码为什么会报错

# encoding:utf-8
import xlrd,xlwt
def stary(names,hight,cole):
    staryer=xlwt.XFStyle
    fone=xlwt.Font
    fone.name=names
    fone.height=hight
    fone.bold=True
    fone.underline=True
    fone.colour_index=cole
    fone.outline=True
    staryer.font=fone
a=input('请选择字体:')
b=input('字体大小:')
c=input('字体颜色:')
book=xlwt.Workbook()
new_sheet=book.add_sheet('新建工作表')
new_sheet2=book.add_sheet('新建工作表2')
book.save('d:\新建工作部.xsl')
wookbook=xlrd.open_workbook('d:\新建工作部.xsl')
sheet=wookbook.sheet_by_index(0)
sheet2=wookbook.sheet_by_index(1)
sheet.write(0,0,'实验',stary(a,b,c))
book.save('d:\新建工作部.xsl')

这是报错信息

请选择字体:'Time New Roman'

字体大小:500

字体颜色:3

Traceback (most recent call last):

  File "D:/S14/python模块/xlrd模块.py", line 23, in <module>

    sheet.write(0,0,'实验',stary(a,b,c))

AttributeError: 'Sheet' object has no attribute 'write'


正在回答

3 回答

sheet对象没有write属性

0 回复 有任何疑惑可以回复我~
# encoding:utf-8
import xlrd,xlwt
def stary(names,hight,cole):
    staryer=xlwt.XFStyle
    nfone=xlwt.Font
    nfone.name=names
    nfone.height=hight
    nfone.bold=True
    nfone.underline=True
    nfone.colour_index=cole
    nfone.outline=True
    staryer.font=nfone
a=input('请选择字体:')
b=input('字体大小:')
c=input('字体颜色:')
book=xlwt.Workbook()
new_sheet=book.add_sheet('新建工作表')
new_sheet2=book.add_sheet('新建工作表2')
book.save('d:\新建工作部.xsl')
wookbook=xlrd.open_workbook('d:\新建工作部.xsl')
sheet=wookbook.sheet_by_index(0)
sheet2=wookbook.sheet_by_index(1)
new_sheet.write(0,0,'实验',stary(a,b,c))
book.save('d:\新建工作部.xsl')

你看看这样我依然出错

C:\Users\25063\Anaconda3\python.exe D:/S14/python模块/xlrd模块.py

请选择字体:Malgun Gothic

字体大小:400

字体颜色:3

Traceback (most recent call last):

  File "D:/S14/python模块/xlrd模块.py", line 23, in <module>

    new_sheet.write(0,0,'实验',stary(a,b,c))

  File "C:\Users\25063\Anaconda3\lib\site-packages\xlwt\Worksheet.py", line 1088, in write

    self.row(r).write(c, label, style)

  File "C:\Users\25063\Anaconda3\lib\site-packages\xlwt\Row.py", line 229, in write

    self.__adjust_height(style)

  File "C:\Users\25063\Anaconda3\lib\site-packages\xlwt\Row.py", line 59, in __adjust_height

    twips = style.font.height

AttributeError: 'NoneType' object has no attribute 'font'


Process finished with exit code 1


0 回复 有任何疑惑可以回复我~

sheet 在这里是不能被使用的,你看发出来的最后一行

1 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
初识Python
  • 参与学习       758868    人
  • 解答问题       8657    个

学python入门视频教程,让你快速入门并能编写简单的Python程序

进入课程

大家看看这段代码为什么会报错

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信