openpyxl with lxml + django + uwsgi failed to generate a right format excel file?
The excel file generated cannot be open when my project started with uwsgi when lxml
installed in my environment as it can be opened successfully with django manage.py runserver
and gunicorn
My main codes like below:
── test_excel
├── urls.py
├── wsgi.py
└── settings.py
── manage.py
urls.py
from django.contrib import admin
from django.urls import path
from django.views import View
from openpyxl.writer.excel import save_virtual_workbook
import pandas as pd
from django.http import HttpResponse, StreamingHttpResponse
import xlrd
import openpyxl
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment
from openpyxl.writer.excel import save_virtual_workbook
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
class TestExcelView(View):
def get(self, request):
wb = Workbook()
ws = wb.active
ws.merge_cells('A1:B1')
a1 = ws["A1"]
ws["A1"] = "reason"
ws.column_dimensions["A"].width = 100
ali = Alignment(horizontal='center', vertical='center')
a1.alignment = ali
ws["A2"] = "request_id"
ws.column_dimensions["A"].width = 50
ws["B2"] = "rebate_reason"
ws.column_dimensions["B"].width = 50
for item in ws["A2:B2"][0]:
item.font = Font(color='FF0000')
item.alignment = ali
response = HttpResponse(save_virtual_workbook(wb))
response["Content-Type"] = 'application/vnd.ms-excel'
response['Content-Disposition'] = 'attachment; filename={}.xlsx'.format("test_excel")
return response
urlpatterns = [
path('admin/', admin.site.urls),
path('test/', TestExcelView.as_view()),
]
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
#os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_excel.settings')
application = get_wsgi_application()
test_excel.ini
[uwsgi]
pythonpath=/home/bluev/work/bv_crm_bak/test_excel
pythonpath=/home/bluev/work/bv_crm_bak/test_excel
env=DJANGO_SETTINGS_MODULE=test_excel.settings
module=test_excel.wsgi
master=True
pidfile=logs/test_excel.pid
vacuum=True
max-requests=100000
enable-threads=true
processes = 4
threads=8
listen=64
daemonize=logs/test_excel.log
log-slow=3000
python-autoreload=1
http=0.0.0.0:8876
Then if i start the project like uwsgi --ini test_excel.ini
as lxml
installed , and request localhost:8876/test/
, i would get a failed excel file. But when I uninstalled lxml
the result is right.
I don't understand why lxml
affects the excel response of django with uwsgi
How can I get the right excel file response with uwsgi
in this situation?
Thanks.
python version: Python 3.6.8
package list:
asgiref==3.2.7
Django==3.0.6
et-xmlfile==1.0.1
jdcal==1.4.1
lxml==4.5.1
mysqlclient==1.4.6
numpy==1.18.4
openpyxl==3.0.3
pandas==1.0.3
python-dateutil==2.8.1
pytz==2020.1
six==1.14.0
sqlparse==0.3.1
uWSGI==2.0.18
xlrd==1.2.0
- Updated different condition detail
environ condition excel_file(if excel file can be opened)
gunicorn + lxml + django yes
gunicorn + django yes
django runserver + lxml yes
django runserver yes
uwsgi + lxml + django no
uwsgi + django yes