为什么我的会报这些错,错误在上面,我写的代码在下边

来源:4-3 Excel写入数据

GEM意

2025-03-31 20:36:12

C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:\Users\admin\PycharmProjects\pythonProject3\4-3.py 
Traceback (most recent call last):
  File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\site-packages\xlsxwriter\worksheet.py", line 551, in _write
    f = float(token)
TypeError: float() argument must be a string or a number, not 'Cell'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\admin\PycharmProjects\pythonProject3\4-3.py", line 35, in <module>
    write(a)
  File "C:\Users\admin\PycharmProjects\pythonProject3\4-3.py", line 31, in write
    book.write(index,sub_index,sub_data)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\site-packages\xlsxwriter\worksheet.py", line 108, in cell_wrapper
    return method(self, *args, **kwargs)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\site-packages\xlsxwriter\worksheet.py", line 481, in write
    return self._write(row, col, *args)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\site-packages\xlsxwriter\worksheet.py", line 556, in _write
    raise TypeError(f"Unsupported type {type(token)} in write()")
TypeError: Unsupported type <class 'xlrd.sheet.Cell'> in write()


import xlsxwriter
import xlrd
def read():
    result=[]
    excel=xlrd.open_workbook("study.xlsx")
    book=excel.sheet_by_name("学生手册")
    for i in book.get_rows():
        content=[]
        for j in i:
            content.append(j)
        result.append(content)
    return result


def write(content):
    excel=xlsxwriter.Workbook("李文.xlsx")
    book=excel.add_worksheet('信息')

    for index,data in enumerate(content):
        for sub_index,sub_data in enumerate(data):
            book.write(index,sub_index,sub_data)
    excel.close()
if __name__=="__main__":
  a=read()
  write(a)


写回答

1回答

好帮手慕小猿

2025-04-01

同学,你好!在read()函数的for 循环中,我们获取每一行数据后,循环这一行的每个单元格,应该取单元格的值,也就是将j.value添加到content中,不是j单元格添加到content中。修改如下
https://img1.sycdn.imooc.com/climg/0fdbaf6709eb31f103150135.jpg

祝学习愉快~

0

Python全能工程师

8阶段5大热门领域,从入门到实战,带你从零基础晋级全能,全面提升就业力

1604 学习 · 968 问题

查看课程