> 文章列表 > Python导出含有中文名文件解决方案

Python导出含有中文名文件解决方案

Python导出含有中文名文件解决方案

使用Python开发过程中有用到需要导出文件的功能

异常代码
# 代码片段
def return_workbook(self, workbook, model_code, x_io):name = “税单.xls”workbook.close()res = HttpResponse()res["Content-Type"] = "application/octet-stream"res["Content-Disposition"] = 'filename="{}.xlsx'.format(name)res.write(x_io.getvalue())return res
导出后文件名异常

Python导出含有中文名文件解决方案

修复后
# 代码片段def return_workbook(self, workbook, model_code, x_io):name = “税单.xls”workbook.close()res = HttpResponse()res["Content-Type"] = "application/octet-stream"from django.utils.encoding import escape_uri_pathres["Content-Disposition"] = 'filename="{}.xlsx'.format(escape_uri_path(name))res.write(x_io.getvalue())return res
导出后文件名正常显示

Python导出含有中文名文件解决方案