@@ -2603,6 +2603,57 @@ def auto_filter(
26032603 if err != "" :
26042604 raise RuntimeError (err )
26052605
2606+ def auto_fit_col_width (self , sheet : str , columns : str ) -> None :
2607+ """
2608+ Auto fit columns width according to their text content with font format.
2609+ If the selected range contains hidden columns and those columns have
2610+ content, this function will unhide the hidden columns. Not that this
2611+ function calculates the width of the text approximately based on the
2612+ font format, currently does not support merged cells. the actual width
2613+ may be different when you open the workbook in Office applications. This
2614+ process can be relatively slow on large worksheets, so this should
2615+ normally only be called once per column, at the end of your processing.
2616+
2617+ Args:
2618+ sheet (str): The worksheet name
2619+ columns (str): The columns range
2620+
2621+ Returns:
2622+ None: Return None if no error occurred, otherwise raise a
2623+ RuntimeError with the message.
2624+
2625+ Example:
2626+ Auto fit column width for column `D` on `Sheet1`:
2627+
2628+ ```python
2629+ try:
2630+ f.auto_fit_col_width("Sheet1", "D")
2631+ except (RuntimeError, TypeError) as err:
2632+ print(err)
2633+ ```
2634+
2635+ Auto fit column width for columns `D` to `F` on `Sheet1`:
2636+
2637+ ```python
2638+ try:
2639+ f.auto_fit_col_width("Sheet1", "D:F")
2640+ except (RuntimeError, TypeError) as err:
2641+ print(err)
2642+ ```
2643+ """
2644+ prepare_args (
2645+ [sheet , columns ],
2646+ [argsRule ("sheet" , [str ]), argsRule ("columns" , [str ])],
2647+ )
2648+ lib .AutoFitColWidth .restype = c_char_p
2649+ err = lib .AutoFitColWidth (
2650+ self .file_index ,
2651+ sheet .encode (ENCODE ),
2652+ columns .encode (ENCODE ),
2653+ ).decode (ENCODE )
2654+ if err != "" :
2655+ raise RuntimeError (err )
2656+
26062657 def calc_cell_value (self , sheet : str , cell : str , * opts : Options ) -> str :
26072658 """
26082659 Get calculated cell value. This feature is currently in working
0 commit comments