2121 c_char ,
2222 c_double ,
2323 c_int ,
24+ c_int32 ,
2425 c_longlong ,
2526 c_ubyte ,
2627 cast ,
@@ -452,19 +453,48 @@ def py_value_to_c_interface(py_value):
452453 """
453454 type_mappings = {
454455 int : lambda : Interface (type = 1 , integer = py_value ),
455- str : lambda : Interface (type = 2 , string = py_value ),
456- float : lambda : Interface (type = 3 , float64 = py_value ),
457- bool : lambda : Interface (type = 4 , boolean = py_value ),
458- datetime : lambda : Interface (type = 5 , integer = int (py_value .timestamp ())),
456+ str : lambda : Interface (type = 3 , string = py_value ),
457+ float : lambda : Interface (type = 4 , float64 = py_value ),
458+ bool : lambda : Interface (type = 5 , boolean = py_value ),
459+ datetime : lambda : Interface (type = 6 , integer = int (py_value .timestamp ())),
459460 date : lambda : Interface (
460- type = 5 ,
461+ type = 6 ,
461462 integer = int (datetime .combine (py_value , time .min ).timestamp ()),
462463 ),
463464 }
464465 interface = type_mappings .get (type (py_value ), Interface )()
465466 return py_value_to_c (interface , types_go ._Interface ())
466467
467468
469+ def c_value_to_py_interface (c_value ):
470+ """
471+ Converts a C value to a Python interface representation.
472+
473+ Args:
474+ c_value: The C value to be converted.
475+
476+ Returns:
477+ An Interface object representing the C value in a Python-compatible format.
478+
479+ Raises:
480+ TypeError: If the type of c_value is not supported.
481+ """
482+ if c_value is None :
483+ return None
484+ type_mappings = {
485+ 1 : lambda : c_value .Integer ,
486+ 2 : lambda : c_value .Integer32 ,
487+ 3 : lambda : c_value .String .decode (ENCODE ) if c_value .String else "" ,
488+ 4 : lambda : c_value .Float64 ,
489+ 5 : lambda : c_value .Boolean ,
490+ 6 : lambda : datetime .fromtimestamp (c_value .Integer ),
491+ }
492+ converter = type_mappings .get (c_value .Type )
493+ if converter :
494+ return converter ()
495+ raise TypeError (f"unsupported interface type code: { c_value .Type } " )
496+
497+
468498def prepare_args (args : List , types : List [argsRule ]):
469499 """
470500 Validate arguments against expected types.
@@ -785,7 +815,7 @@ def set_panes(self, opts: Panes) -> None:
785815 def set_row (
786816 self ,
787817 cell : str ,
788- values : List [Union [None , int , str , bool , datetime , date ]],
818+ values : List [Union [bool , float , int , str , date , datetime , None ]],
789819 ) -> None :
790820 """
791821 Writes an array to stream rows by giving starting cell reference and a
@@ -794,8 +824,8 @@ def set_row(
794824
795825 Args:
796826 cell (str): The cell reference
797- values (List[Union[None, int, str, bool , datetime, date ]]): The cell
798- values
827+ values (List[Union[bool, float, int, str, date , datetime, None ]]):
828+ The cell values
799829
800830 Returns:
801831 None: Return None if no error occurred, otherwise raise a
@@ -3054,6 +3084,30 @@ def get_comments(self, sheet: str) -> List[Comment]:
30543084 raise RuntimeError (err )
30553085 return result .comments if result and result .comments else []
30563086
3087+ def get_custom_props (self ) -> List [CustomProperty ]:
3088+ """
3089+ Get custom file properties
3090+
3091+ Returns:
3092+ List[CustomProperty]: Return the custom file properties if no error
3093+ occurred, otherwise raise a RuntimeError with the message.
3094+ """
3095+ lib .GetCustomProps .restype = types_go ._GetCustomPropsResult
3096+ res = lib .GetCustomProps (self .file_index )
3097+ err = res .Err .decode (ENCODE )
3098+ if err == "" :
3099+ arr = []
3100+ if res .CustomProps :
3101+ for i in range (res .CustomPropsLen ):
3102+ arr .append (
3103+ CustomProperty (
3104+ name = res .CustomProps [i ].Name .decode (ENCODE ),
3105+ value = c_value_to_py_interface (res .CustomProps [i ].Value ),
3106+ )
3107+ )
3108+ return arr
3109+ raise RuntimeError (err )
3110+
30573111 def get_default_font (self ) -> str :
30583112 """
30593113 Get the default font name currently set in the workbook. The spreadsheet
@@ -5148,6 +5202,37 @@ def set_conditional_format(
51485202 if err != "" :
51495203 raise RuntimeError (err )
51505204
5205+ def set_custom_props (self , prop : CustomProperty ) -> None :
5206+ """
5207+ Set custom file properties by given property name and value. If the
5208+ property name already exists, it will be updated, otherwise a new
5209+ property will be added. The value can be of type `int`, `float`, `bool`,
5210+ `str`, `datetime`, `date` and `None`. The property will be delete if the
5211+ value is `None`. The function returns an error if the property value is
5212+ not of the correct type.
5213+
5214+ Args:
5215+ prop (CustomProperty): The custom file property
5216+
5217+ Returns:
5218+ None: Return None if no error occurred, otherwise raise a
5219+ RuntimeError with the message.
5220+ """
5221+ prepare_args ([prop ], [argsRule ("prop" , [CustomProperty ])])
5222+ lib .SetCustomProps .restype = c_char_p
5223+ options = types_go ._CustomProperty ()
5224+ setattr (options , "Name" , (prop .name or "" ).encode (ENCODE ))
5225+ val = types_go ._Interface ()
5226+ if type (prop .value ) is int :
5227+ setattr (val , "Type" , c_int (2 ))
5228+ setattr (val , "Integer32" , c_int32 (prop .value ))
5229+ else :
5230+ val = py_value_to_c_interface (prop .value )
5231+ setattr (options , "Value" , val )
5232+ err = lib .SetCustomProps (self .file_index , options ).decode (ENCODE )
5233+ if err != "" :
5234+ raise RuntimeError (err )
5235+
51515236 def set_default_font (self , font_name : str ) -> None :
51525237 """
51535238 Set the default font name in the workbook. The spreadsheet generated by
@@ -5216,10 +5301,7 @@ def set_defined_name(self, defined_name: DefinedName) -> None:
52165301 ))
52175302 ```
52185303 """
5219- prepare_args (
5220- [defined_name ],
5221- [argsRule ("defined_name" , [DefinedName ])],
5222- )
5304+ prepare_args ([defined_name ], [argsRule ("defined_name" , [DefinedName ])])
52235305 lib .SetDefinedName .restype = c_char_p
52245306 options = py_value_to_c (defined_name , types_go ._DefinedName ())
52255307 err = lib .SetDefinedName (self .file_index , byref (options )).decode (ENCODE )
@@ -5645,7 +5727,7 @@ def set_sheet_col(
56455727 self ,
56465728 sheet : str ,
56475729 cell : str ,
5648- values : List [Union [None , int , str , bool , datetime , date ]],
5730+ values : List [Union [bool , float , int , str , date , datetime , None ]],
56495731 ) -> None :
56505732 """
56515733 Writes cells to column by given worksheet name, starting cell reference
@@ -5654,8 +5736,8 @@ def set_sheet_col(
56545736 Args:
56555737 sheet (str): The worksheet name
56565738 cell (str): The cell reference
5657- values (List[Union[None, int, str, bool , datetime, date]]): The cell
5658- values
5739+ values (List[Union[bool, float, int, str, date , datetime, None]):
5740+ The cell values
56595741
56605742 Returns:
56615743 None: Return None if no error occurred, otherwise raise a
@@ -5780,7 +5862,7 @@ def set_sheet_row(
57805862 self ,
57815863 sheet : str ,
57825864 cell : str ,
5783- values : List [Union [None , int , str , bool , datetime , date ]],
5865+ values : List [Union [bool , float , int , str , date , datetime , None ]],
57845866 ) -> None :
57855867 """
57865868 Writes cells to row by given worksheet name, starting cell reference and
@@ -5789,8 +5871,8 @@ def set_sheet_row(
57895871 Args:
57905872 sheet (str): The worksheet name
57915873 cell (str): The cell reference
5792- values (List[Union[None, int, str, bool , datetime, date ]]): The cell
5793- values
5874+ values (List[Union[bool, float, int, str, date , datetime, None ]]):
5875+ The cell values
57945876
57955877 Returns:
57965878 None: Return None if no error occurred, otherwise raise a
0 commit comments