我正在尝试自动调整 Dataframe 中每个标题的长度,同时将其转换为EXCEL文件,如下所示:
from pandas import ExcelWriter
for k,v in final_1.items():
v.to_excel(writer, sheet_name=k, index=False)
for column in v:
column_length = max(v[column].astype(str).map(len).max(), len(column))
col_idx = v.columns.get_loc(column)
writer.sheets[k].set_column(col_idx, col_idx, column_length+1)
其中final_1
是 Dataframe 词典。
这在本地服务器上运行良好,但在部署到AWS时显示以下错误:
“WorkSheet”对象没有属性“Set_Column”
完整的回溯:
Traceback (most recent call last):
File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args,**callback_kwargs)
File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args,**kwargs)
File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args,**kwargs)
File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args,**kwargs)
File "/var/app/current/inventory/RFIDReport.py", line 204, in get
writer.sheets[k].set_column(col_idx, col_idx, column_length+1)
Exception Type: AttributeError at /rfid-dumpdownload/
Exception Value: 'Worksheet' object has no attribute 'set_column'
我无法理解为什么会发生这种情况,因为两台服务器都有相同版本的python
和pandas
1条答案
按热度按时间hfyxw5xn1#
Pandas可以使用Openpyxl或xlsxWriter作为“引擎”来创建带有
to_excel()
的xlsx文件。set_column()
方法是一个xlsxWriter方法,但是关于缺少方法/属性的错误消息表明Pandas没有使用它,可能是因为它没有安装,而可能使用的是Openpyxl。为了避免在创建
writer
对象时显式指定所需的xlsx引擎:您可能还需要安装xlsxWriter。这可能就是AWS服务器和测试服务器之间的区别。