diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index 41e0b754cfa81..c415616affcd5 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -489,6 +489,7 @@ styler.sparse.columns True "Sparsify" MultiIndex displ in Styler output. styler.render.max_elements 262144 Maximum number of datapoints that Styler will render trimming either rows, columns or both to fit. +styler.render.encoding utf-8 Default encoding for output HTML or LaTeX files. styler.format.formatter None Object to specify formatting functions to ``Styler.format``. styler.format.na_rep None String representation for missing data. styler.format.precision 6 Precision to display floating point and complex numbers. diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 57f36d1599afb..aef6128c63829 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -75,7 +75,7 @@ Styler - :meth:`.Styler.to_latex` introduces keyword argument ``environment``, which also allows a specific "longtable" entry through a separate jinja2 template (:issue:`41866`). - :meth:`.Styler.to_html` introduces keyword arguments ``sparse_index`` and ``sparse_columns`` (:issue:`41946`) - Keyword argument ``level`` is added to :meth:`.Styler.hide_index` and :meth:`.Styler.hide_columns` for optionally controlling hidden levels in a MultiIndex (:issue:`25475`) - - Global options have been extended to configure default ``Styler`` properties including formatting options (:issue:`41395`) + - Global options have been extended to configure default ``Styler`` properties including formatting and encoding options (:issue:`41395`) There are also bug fixes and deprecations listed below. diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 89f3bc76d2905..a6aa285f538d3 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -793,6 +793,11 @@ def register_converter_cb(key): A formatter object to be used as default within ``Styler.format``. """ +styler_encoding = """ +: str + The encoding used for output HTML and LaTeX files. +""" + with cf.config_prefix("styler"): cf.register_option("sparse.index", True, styler_sparse_index_doc, validator=bool) @@ -807,6 +812,8 @@ def register_converter_cb(key): validator=is_nonnegative_int, ) + cf.register_option("render.encoding", "utf-8", styler_encoding, validator=is_str) + cf.register_option("format.decimal", ".", styler_decimal, validator=is_str) cf.register_option( diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 7c3d7fe57b7b1..4fc13fd54df02 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -526,8 +526,9 @@ def to_latex( rendered. .. versionadded:: 1.4.0 - encoding : str, default "utf-8" - Character encoding setting. + encoding : str, optional + Character encoding setting. Defaults + to ``pandas.options.styler.render.encoding`` value of "utf-8". convert_css : bool, default False Convert simple cell-styles from CSS to LaTeX format. Any CSS not found in conversion table is dropped. A style can be forced by adding option @@ -849,7 +850,10 @@ def to_latex( convert_css=convert_css, ) - return save_to_buffer(latex, buf=buf, encoding=encoding) + encoding = encoding or get_option("styler.render.encoding") + return save_to_buffer( + latex, buf=buf, encoding=None if buf is None else encoding + ) def to_html( self, @@ -898,8 +902,8 @@ def to_html( .. versionadded:: 1.4.0 encoding : str, optional - Character encoding setting for file output, and HTML meta tags, - defaults to "utf-8" if None. + Character encoding setting for file output, and HTML meta tags. + Defaults to ``pandas.options.styler.render.encoding`` value of "utf-8". doctype_html : bool, default False Whether to output a fully structured HTML file including all HTML elements, or just the core ``