鱼C论坛

 找回密码
 立即注册
查看: 1653|回复: 3

[已解决]openpyxl 打开文件报错 大佬麻烦看看

[复制链接]
发表于 2023-7-25 15:04:56 | 显示全部楼层 |阅读模式
5鱼币
from openpyxl import load_workbook
def zzzz(request):
    # if request.method == "GET":
    #     return render(request, "zi.html")
    fff = request.FILES.get("aaa")
    print(type(fff))
    wb = load_workbook(fff)
    print(wb)
    she = wb.worksheets[0]
    print("she",she)
    cell = she.cell(1,1)
    print(cell)
    return render(request, "zi.html")
报错截图 6%~D{9X~H)J3P3BBYFWF.png


大佬麻烦看看这个
最佳答案
2023-7-25 15:04:57
看起来路径没传对,传了一个None类型,也就是你的请求中没有名字为 aaa 的 files

最佳答案

查看完整内容

看起来路径没传对,传了一个None类型,也就是你的请求中没有名字为 aaa 的 files
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-7-25 15:04:57 | 显示全部楼层    本楼为最佳答案   
看起来路径没传对,传了一个None类型,也就是你的请求中没有名字为 aaa 的 files
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-7-25 17:38:26 | 显示全部楼层
from openpyxl import load_workbook
def zzzz(request):
    # if request.method == "GET":
    #     return render(request, "zi.html")
    fff = request.FILES.get("aaa")
    print(type(fff))
    wb = load_workbook(fff)
    print(wb)
    she = wb.worksheets[0]
    print("she",she)
    cell = she.cell(1,1)
    print(cell)
    return render(request, "zi.html")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-7-25 18:22:39 | 显示全部楼层
你的错误显示为  TypeError:expected str,bytes or os.PathLike object,not NoneType ,这个错误说明  load_workbook  函数尝试接收一个字符串、字节或 os.PathLike 对象,但是它接收到的是 None。

从你的代码来看,你是用  request.FILES.get("aaa")  尝试获取上传的文件。错误可能出在这里, request.FILES.get("aaa")  可能返回了 None,也就是说它没有成功地获取到文件。因此当你尝试用  load_workbook  打开这个文件时,它实际上在尝试打开一个 None,这就导致了你看到的错误。

为了解决这个问题,你需要确保你在  request.FILES  中确实有一个名为 "aaa" 的文件。你可以通过以下方式检查:
def zzzz(request):
    if 'aaa' not in request.FILES:
        print("No file uploaded with name 'aaa'")
        return render(request, "zi.html")

    fff = request.FILES.get("aaa")
    print(type(fff))
    wb = load_workbook(fff)
    print(wb)
    she = wb.worksheets[0]
    print("she",she)
    cell = she.cell(1,1)
    print(cell)
    return render(request, "zi.html")

上述代码会先检查  request.FILES  中是否包含一个名为 "aaa" 的文件,如果没有,就会打印一个错误信息并返回。

另外,根据 openpyxl 的文档, load_workbook  函数接受一个文件名字符串作为参数。如果你试图直接传入文件对象,可能会导致问题。你可能需要先将文件保存到磁盘上,然后传入文件名,或者尝试读取文件的内容然后使用 BytesIO 对象。

比如你可以试试这样的方式:
import io
from openpyxl import load_workbook

def zzzz(request):
    if 'aaa' not in request.FILES:
        print("No file uploaded with name 'aaa'")
        return render(request, "zi.html")

    fff = request.FILES.get("aaa")
    file_content = fff.read()
    wb = load_workbook(filename=io.BytesIO(file_content))
    print(wb)
    she = wb.worksheets[0]
    print("she",she)
    cell = she.cell(1,1)
    print(cell)
    return render(request, "zi.html")

上述代码会先从上传的文件中读取所有的内容,然后用  BytesIO  创建一个类文件对象,并将这个对象传递给  load_workbook  函数。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 09:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表