PydPiper / pylightxl

A light weight, zero dependency, minimal functionality excel read/writer python library
https://pylightxl.readthedocs.io
MIT License
302 stars 47 forks source link

better add `encoding='utf-8'` in every `with open(some_path, 'w') as f` statements #97

Open debbiezhu opened 1 year ago

debbiezhu commented 1 year ago

Pylightxl Version: 1.61 Python Version: 3.1

Summary of Bug/Feature: when try to add several work sheets of Chinese content, it raises an error in writexl_alt_writer()

Traceback:

C:\Users\dz\venv\Scripts\python.exe C:\Users\dz\lilpine\buggy_open.py 
Traceback (most recent call last):
  File "C:\Users\dz\lilpine\buggy_open.py", line 31, in <module>
    xl.writexl(db=db, fn=path_excel)
  File "C:\Users\dz\lilpine\venv\lib\site-packages\pylightxl\pylightxl.py", line 639, in writexl
    writexl_alt_writer(db, fn)
  File "C:\Users\dz\lilpine\venv\lib\site-packages\pylightxl\pylightxl.py", line 724, in writexl_alt_writer
    f.write(text)
  File "C:\Users\dz\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 166-168: character maps to <undefined>

Process finished with exit code 1

Suggestion for fix:

    with open(temp_folder + '/xl/sharedStrings.xml', 'w', encoding='utf-8') as f:
        f.write(text)