elapouya / python-docx-template

Use a docx as a jinja2 template
GNU Lesser General Public License v2.1
1.92k stars 378 forks source link

UnicodeEncodeError at Exception Location: /usr/lib/python3.9/zipfile.py #518

Closed ant1fr1z closed 7 months ago

ant1fr1z commented 7 months ago

I'm transferring the project from Windows to Raspberry Pi. On Windows the code works without problems, on Raspberry there is an error, I can’t understand what’s wrong

Here is the type of error in django

UnicodeEncodeError at /documents/create 'ascii' codec can't encode characters in position 21-30: ordinal not in range(128) Request Method: POST Request URL: http://localhost/documents/create Django Version: 3.1.4 Exception Type: UnicodeEncodeError Exception Value:
'ascii' codec can't encode characters in position 21-30: ordinal not in range(128) Exception Location: /usr/lib/python3.9/zipfile.py, line 1239, in init Python Executable: /home/remote/test/test2env/bin/python Python Version: 3.9.2 Python Path:
['/home/remote/test', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/home/remote/test/test2env/lib/python3.9/site-packages', '/home/remote/test/Django', '/home/remote/test/test2env'] Server time: Thu, 23 Nov 2023 20:50:35 +0000

Unicode error hint The string that could not be encoded/decoded was: ok_Петренко_23_1

As you can see the error occurs in a place where there are both Latin and Cyrillic symbols. This is part of the code, when saving a file I get this error, on Windows everything works without problems

for user in users:
        doc = DocxTemplate(settings.DOCX_TEMPLATE_URL + "mydoc.docx")
        context = {'cur_date': cur_date, 'nachalo': nachalo.strftime('%d.%m.%Y'), 'konec': konec.strftime('%d.%m.%Y'), 'days': days, 'kyda': destination, 'rank': user.rank, 'pos': user.position, 'fn': user.firstname, 'mn': user.middlename, 'sn': user.secondname, 'n_up': nac.position, 'n_up_z': nac.rank, 'n_up_fn': nac.firstname, 'n_up_sn': nac.secondname.upper()}
        doc.render(context)
        doc.save(settings.DOCX_SAVE_URL + 'mydoc_' + user.secondname + today + '.docx')

on Windows locale.getdefaultlocale() ('ru_RU', 'cp1251') sys.getdefaultencoding() 'utf-8'

on Raspberry I tried similar parameters, and changed locale.getdefaultlocale() to utf-8, no change, same error

ant1fr1z commented 7 months ago

solved it in the file /etc/apache2/envvars put export LANG='ru_RU.UTF-8'