Amazon Linux (RedHat/CentOS) EC2 上的 openpyxl "BadZipFile"
openpyxl "BadZipFile" on Amazon Linux (RedHat/CentOS) EC2
我必须读取一个 .xlsx 文件,填充一些单元格并保存编辑后的副本,所以我执行了以下操作:
- 使用 wget:
从 S3 将文件保存在我的 django 项目根文件夹中
$ sudo wget https://s3.console.aws.amazon.com/s3/buckets/path/to/file.xlsx
- 读取、填写并保存:
import openpyxl
wb = openpyxl.load_workbook('/path/to/file.xlsx', keep_vba = True)
sheet = wb['Sheet1']
sheet['A1'] = "My Text"
wb.save('/path/to/result.xlsx')
当我在 Windows10 上的本地计算机上 运行 时,它工作正常。情况是我必须将它部署到我的 EC2 实例,这是一台 Amazon Linux (RedHat/CentOS) 机器。所以,当我在 EC2 上 运行 时,我得到了以下回溯:
File "/path/to/venv/lib64/python3.6/dist-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/path/to/venv/lib64/python3.6/dist-packages/django/core/handlers/base.py" in _get_response
111. resolver_match = resolver.resolve(request.path_info)
File "/path/to/venv/lib64/python3.6/dist-packages/django/urls/resolvers.py" in resolve
491. for pattern in self.url_patterns:
File "/path/to/venv/lib64/python3.6/dist-packages/django/utils/functional.py" in __get__
37. res = instance.__dict__[self.name] = self.func(instance)
File "/path/to/venv/lib64/python3.6/dist-packages/django/urls/resolvers.py" in url_patterns
533. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/path/to/venv/lib64/python3.6/dist-packages/django/utils/functional.py" in __get__
37. res = instance.__dict__[self.name] = self.func(instance)
File "/path/to/venv/lib64/python3.6/dist-packages/django/urls/resolvers.py" in urlconf_module
526. return import_module(self.urlconf_name)
File "/usr/lib64/python3.6/importlib/__init__.py" in import_module
126. return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>" in _gcd_import
994. <source code not available>
File "<frozen importlib._bootstrap>" in _find_and_load
971. <source code not available>
File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
955. <source code not available>
File "<frozen importlib._bootstrap>" in _load_unlocked
665. <source code not available>
File "<frozen importlib._bootstrap_external>" in exec_module
678. <source code not available>
File "<frozen importlib._bootstrap>" in _call_with_frames_removed
219. <source code not available>
File "/path/to/project/mysite/urls.py" in <module>
41. url(r'^product/app/', include('app.urls')),
File "/path/to/venv/lib64/python3.6/dist-packages/django/urls/conf.py" in include
34. urlconf_module = import_module(urlconf_module)
File "/usr/lib64/python3.6/importlib/__init__.py" in import_module
126. return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>" in _gcd_import
994. <source code not available>
File "<frozen importlib._bootstrap>" in _find_and_load
971. <source code not available>
File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
955. <source code not available>
File "<frozen importlib._bootstrap>" in _load_unlocked
665. <source code not available>
File "<frozen importlib._bootstrap_external>" in exec_module
678. <source code not available>
File "<frozen importlib._bootstrap>" in _call_with_frames_removed
219. <source code not available>
File "/path/to/project/app/urls.py" in <module>
1. from . import views
File "/path/to/project/app/views.py" in <module>
39. from .readexcel_function import *
File "/path/to/project/app/readexcel_function .py" in <module>
10. wb = openpyxl.load_workbook('/path/to/file.xlsx', keep_vba = True)
File "/path/to/venv/lib64/python3.6/dist-packages/openpyxl/reader/excel.py" in load_workbook
313. data_only, keep_links)
File "/path/to/venv/lib64/python3.6/dist-packages/openpyxl/reader/excel.py" in __init__
124. self.archive = _validate_archive(fn)
File "/path/to/venv/lib64/python3.6/dist-packages/openpyxl/reader/excel.py" in _validate_archive
96. archive = ZipFile(filename, 'r')
File "/usr/lib64/python3.6/zipfile.py" in __init__
1131. self._RealGetContents()
File "/usr/lib64/python3.6/zipfile.py" in _RealGetContents
1198. raise BadZipFile("File is not a zip file")
Exception Type: BadZipFile at /
Exception Value: File is not a zip file
我在 Ubuntu 上尝试 运行 并得到了同样的异常。
我也试过:
- 升级openpyxl;
- 使用 zipfile lib 从 zip 中读取;
相同的消息。
在 Linux 上是否可行,或者我必须在 Windows 服务器 EC2 上部署我的项目?
经过数小时的深入研究后得出的结论是,如果 OS 不支持 excel,则无法使其正常工作。毕竟我不得不将它部署在 Windows Server EC2 实例上。
编辑
我认为即使没有 excel for Ubuntu/Amazon Linux.
也可以只操作“.xlsx”文件
编辑结束
也许有一天我们会有一个版本,然后为它更新 openpyxl。
我必须读取一个 .xlsx 文件,填充一些单元格并保存编辑后的副本,所以我执行了以下操作:
- 使用 wget: 从 S3 将文件保存在我的 django 项目根文件夹中
$ sudo wget https://s3.console.aws.amazon.com/s3/buckets/path/to/file.xlsx
- 读取、填写并保存:
import openpyxl
wb = openpyxl.load_workbook('/path/to/file.xlsx', keep_vba = True)
sheet = wb['Sheet1']
sheet['A1'] = "My Text"
wb.save('/path/to/result.xlsx')
当我在 Windows10 上的本地计算机上 运行 时,它工作正常。情况是我必须将它部署到我的 EC2 实例,这是一台 Amazon Linux (RedHat/CentOS) 机器。所以,当我在 EC2 上 运行 时,我得到了以下回溯:
File "/path/to/venv/lib64/python3.6/dist-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/path/to/venv/lib64/python3.6/dist-packages/django/core/handlers/base.py" in _get_response
111. resolver_match = resolver.resolve(request.path_info)
File "/path/to/venv/lib64/python3.6/dist-packages/django/urls/resolvers.py" in resolve
491. for pattern in self.url_patterns:
File "/path/to/venv/lib64/python3.6/dist-packages/django/utils/functional.py" in __get__
37. res = instance.__dict__[self.name] = self.func(instance)
File "/path/to/venv/lib64/python3.6/dist-packages/django/urls/resolvers.py" in url_patterns
533. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/path/to/venv/lib64/python3.6/dist-packages/django/utils/functional.py" in __get__
37. res = instance.__dict__[self.name] = self.func(instance)
File "/path/to/venv/lib64/python3.6/dist-packages/django/urls/resolvers.py" in urlconf_module
526. return import_module(self.urlconf_name)
File "/usr/lib64/python3.6/importlib/__init__.py" in import_module
126. return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>" in _gcd_import
994. <source code not available>
File "<frozen importlib._bootstrap>" in _find_and_load
971. <source code not available>
File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
955. <source code not available>
File "<frozen importlib._bootstrap>" in _load_unlocked
665. <source code not available>
File "<frozen importlib._bootstrap_external>" in exec_module
678. <source code not available>
File "<frozen importlib._bootstrap>" in _call_with_frames_removed
219. <source code not available>
File "/path/to/project/mysite/urls.py" in <module>
41. url(r'^product/app/', include('app.urls')),
File "/path/to/venv/lib64/python3.6/dist-packages/django/urls/conf.py" in include
34. urlconf_module = import_module(urlconf_module)
File "/usr/lib64/python3.6/importlib/__init__.py" in import_module
126. return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>" in _gcd_import
994. <source code not available>
File "<frozen importlib._bootstrap>" in _find_and_load
971. <source code not available>
File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
955. <source code not available>
File "<frozen importlib._bootstrap>" in _load_unlocked
665. <source code not available>
File "<frozen importlib._bootstrap_external>" in exec_module
678. <source code not available>
File "<frozen importlib._bootstrap>" in _call_with_frames_removed
219. <source code not available>
File "/path/to/project/app/urls.py" in <module>
1. from . import views
File "/path/to/project/app/views.py" in <module>
39. from .readexcel_function import *
File "/path/to/project/app/readexcel_function .py" in <module>
10. wb = openpyxl.load_workbook('/path/to/file.xlsx', keep_vba = True)
File "/path/to/venv/lib64/python3.6/dist-packages/openpyxl/reader/excel.py" in load_workbook
313. data_only, keep_links)
File "/path/to/venv/lib64/python3.6/dist-packages/openpyxl/reader/excel.py" in __init__
124. self.archive = _validate_archive(fn)
File "/path/to/venv/lib64/python3.6/dist-packages/openpyxl/reader/excel.py" in _validate_archive
96. archive = ZipFile(filename, 'r')
File "/usr/lib64/python3.6/zipfile.py" in __init__
1131. self._RealGetContents()
File "/usr/lib64/python3.6/zipfile.py" in _RealGetContents
1198. raise BadZipFile("File is not a zip file")
Exception Type: BadZipFile at /
Exception Value: File is not a zip file
我在 Ubuntu 上尝试 运行 并得到了同样的异常。
我也试过:
- 升级openpyxl;
- 使用 zipfile lib 从 zip 中读取;
相同的消息。
在 Linux 上是否可行,或者我必须在 Windows 服务器 EC2 上部署我的项目?
经过数小时的深入研究后得出的结论是,如果 OS 不支持 excel,则无法使其正常工作。毕竟我不得不将它部署在 Windows Server EC2 实例上。
编辑
我认为即使没有 excel for Ubuntu/Amazon Linux.
也可以只操作“.xlsx”文件编辑结束
也许有一天我们会有一个版本,然后为它更新 openpyxl。