【发布时间】:2020-05-11 20:38:36
【问题描述】:
我必须读取一个 .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')
当我在 Windows 10 上的本地计算机上运行时,它运行良好。情况是我必须将它部署到我的 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 库的 zip 文件中读取;
同样的信息。
在 Linux 上是否可行,或者我必须在 Windows Server EC2 上部署我的项目?
【问题讨论】:
标签: amazon-web-services amazon-ec2 python-3.6 openpyxl django-2.1