Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding issue on windows when saving a table #440

Open
bart-r-willems opened this issue Sep 12, 2024 · 3 comments
Open

Encoding issue on windows when saving a table #440

bart-r-willems opened this issue Sep 12, 2024 · 3 comments

Comments

@bart-r-willems
Copy link

Description

When saving a table with the save message, an exception occurs in Windows.

Reproducible example

  • Run pandas/jupyter on a windows maching
  • create a simple table
  • try saving e.g. df.pipe(GT).save('test.pdf')

The bottom of the stack trace shows this

File D:\Python\Python312\Lib\site-packages\great_tables\_export.py:310, in save(self, file, selector, scale, expand, web_driver, window_size, debug_port, _debug_dump)
    303 with (
    304     tempfile.TemporaryDirectory() as tmp_dir,
    305     wdriver(options=wd_options) as headless_browser,
    306 ):
    307 
    308     # Write the HTML content to the temp file
    309     with open(f"{tmp_dir}/table.html", "w") as temp_file:
--> 310         temp_file.write(html_content)
    312     # Open the HTML file in the headless browser
    313     headless_browser.set_window_size(window_size[0], window_size[1])

File D:\Python\Python312\Lib\encodings\cp1252.py:19, in IncrementalEncoder.encode(self, input, final)
     18 def encode(self, input, final=False):
---> 19     return codecs.charmap_encode(input,self.errors,encoding_table)[0]

UnicodeEncodeError: 'charmap' codec can't encode character '\u2212' in position 7916: character maps to <undefined>

Note that the error occurs due to CP1252 encoding which is the default. I fixed it by adding encoding='utf-8' ib line 309 of the _export.py file:

    # Write the HTML content to the temp file
    with open(f"{tmp_dir}/table.html", "w", encoding='utf-8') as temp_file:
        temp_file.write(html_content)

(Sorry I'm not comfortable with pull requests I don't want to ruin anything)

Expected result

A file test.pdf should be created

Development environment

  • Operating System: Windows 10, Python 3.12.4
  • great_tables Version: 0.11.0
@HansMellman
Copy link

Encountering the same issue when trying to utilize a Unicode character in the heading section (in lieu of an image). Unable to figure out a workaround yet without just removing the character.

@bart-r-willems
Copy link
Author

bart-r-willems commented Sep 14, 2024 via email

@HansMellman
Copy link

You sir, are wonderful. Thank you for that - did the trick perfectly! Hopefully they'll be able to implement the fix natively.

@jrycw jrycw mentioned this issue Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants