我是学生。
我想用openpyxl读取受保护的excel文件,但不能。
这是我的代码。
wb = load_workbook(file_path) # Error returned in this line.
wb.security.workbook_password = 'password'
ws = wb.active
这是我的错误消息
Traceback (most recent call last):
File "D:\env\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "D:\env\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\env\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\env\lib\site-packages\django\views\generic\base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "D:\env\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "D:\manager\views_payment.py", line 32, in post
self.read_excel(tmp.payment_excel)
File "D:\manager\views_payment.py", line 20, in read_excel
wb = load_workbook(file_path)
File "D:\env\lib\site-packages\openpyxl\reader\excel.py", line 171, in load_workbook
archive = _validate_archive(filename)
File "D:\env\lib\site-packages\openpyxl\reader\excel.py", line 121, in _validate_archive
archive = ZipFile(f, 'r', ZIP_DEFLATED)
File "c:\python36\Lib\zipfile.py", line 1100, in __init__
self._RealGetContents()
File "c:\python36\Lib\zipfile.py", line 1168, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
出什么问题了?
答案 0 :(得分:0)
要使用openpyxl打开excel文件,您需要使用未加密的格式(不支持使用openpyxl打开受密码保护的Excel文件),因此您需要手动打开文件并将其保存为未加密的格式首先,在使用openpyxl调用之前。