@@ -1190,6 +1190,73 @@ def to_html(
1190
1190
html , buf = buf , encoding = (encoding if buf is not None else None )
1191
1191
)
1192
1192
1193
+ def to_string (
1194
+ self ,
1195
+ buf = None ,
1196
+ * ,
1197
+ encoding = None ,
1198
+ sparse_index : bool | None = None ,
1199
+ sparse_columns : bool | None = None ,
1200
+ max_rows : int | None = None ,
1201
+ max_columns : int | None = None ,
1202
+ delimiter : str = " " ,
1203
+ ):
1204
+ """
1205
+ Write Styler to a file, buffer or string in text format.
1206
+
1207
+ .. versionadded:: 1.5.0
1208
+
1209
+ Parameters
1210
+ ----------
1211
+ buf : str, Path, or StringIO-like, optional, default None
1212
+ Buffer to write to. If ``None``, the output is returned as a string.
1213
+ encoding : str, optional
1214
+ Character encoding setting for file output.
1215
+ Defaults to ``pandas.options.styler.render.encoding`` value of "utf-8".
1216
+ sparse_index : bool, optional
1217
+ Whether to sparsify the display of a hierarchical index. Setting to False
1218
+ will display each explicit level element in a hierarchical key for each row.
1219
+ Defaults to ``pandas.options.styler.sparse.index`` value.
1220
+ sparse_columns : bool, optional
1221
+ Whether to sparsify the display of a hierarchical index. Setting to False
1222
+ will display each explicit level element in a hierarchical key for each
1223
+ column. Defaults to ``pandas.options.styler.sparse.columns`` value.
1224
+ max_rows : int, optional
1225
+ The maximum number of rows that will be rendered. Defaults to
1226
+ ``pandas.options.styler.render.max_rows``, which is None.
1227
+ max_columns : int, optional
1228
+ The maximum number of columns that will be rendered. Defaults to
1229
+ ``pandas.options.styler.render.max_columns``, which is None.
1230
+
1231
+ Rows and columns may be reduced if the number of total elements is
1232
+ large. This value is set to ``pandas.options.styler.render.max_elements``,
1233
+ which is 262144 (18 bit browser rendering).
1234
+ delimiter : str, default single space
1235
+ The separator between data elements.
1236
+
1237
+ Returns
1238
+ -------
1239
+ str or None
1240
+ If `buf` is None, returns the result as a string. Otherwise returns `None`.
1241
+ """
1242
+ obj = self ._copy (deepcopy = True )
1243
+
1244
+ if sparse_index is None :
1245
+ sparse_index = get_option ("styler.sparse.index" )
1246
+ if sparse_columns is None :
1247
+ sparse_columns = get_option ("styler.sparse.columns" )
1248
+
1249
+ text = obj ._render_string (
1250
+ sparse_columns = sparse_columns ,
1251
+ sparse_index = sparse_index ,
1252
+ max_rows = max_rows ,
1253
+ max_cols = max_columns ,
1254
+ delimiter = delimiter ,
1255
+ )
1256
+ return save_to_buffer (
1257
+ text , buf = buf , encoding = (encoding if buf is not None else None )
1258
+ )
1259
+
1193
1260
def set_td_classes (self , classes : DataFrame ) -> Styler :
1194
1261
"""
1195
1262
Set the DataFrame of strings added to the ``class`` attribute of ``<td>``
0 commit comments