@@ -313,7 +313,8 @@ def py_value_to_c(py_instance, ctypes_instance):
313
313
py_value_to_c (getattr (py_instance , py_field_name ), c_type ()),
314
314
)
315
315
else :
316
- if get_origin (py_field_args [0 ]) is not list :
316
+ arg_type = py_field_args [0 ]
317
+ if get_origin (arg_type ) is not list and arg_type is not bytes :
317
318
# Pointer of the Go data type, for example: *excelize.Options or *string
318
319
value = getattr (py_instance , py_field_name )
319
320
c_type = get_c_field_type (ctypes_instance , c_field_name )._type_
@@ -335,7 +336,15 @@ def py_value_to_c(py_instance, ctypes_instance):
335
336
else :
336
337
# The Go data type array, for example:
337
338
# []*excelize.Options, []excelize.Options, []string, []*string
338
- py_field_type = get_args (py_field_args [0 ])[0 ]
339
+ if arg_type is bytes : # []byte
340
+ c_type = get_c_field_type (ctypes_instance , c_field_name )._type_
341
+ value = getattr (py_instance , py_field_name )
342
+ ctypes_instance .__setattr__ (
343
+ c_field_name , cast (value , POINTER (c_ubyte ))
344
+ )
345
+ ctypes_instance .__setattr__ (c_field_name + "Len" , c_int (len (value )))
346
+ continue
347
+ py_field_type = get_args (arg_type )[0 ]
339
348
if type (None ) not in get_args (py_field_type ):
340
349
# The Go data type array, for example: []excelize.Options or []string
341
350
c_type = get_c_field_type (ctypes_instance , c_field_name )._type_
@@ -616,6 +625,36 @@ def add_picture(
616
625
).decode (ENCODE )
617
626
return None if err == "" else Exception (err )
618
627
628
+ def add_picture_from_bytes (
629
+ self , sheet : str , cell : str , picture : Picture
630
+ ) -> Optional [Exception ]:
631
+ """
632
+ Add picture in a sheet by given picture format set (such as offset,
633
+ scale, aspect ratio setting and print settings), file base name,
634
+ extension name and file bytes, supported image types: EMF, EMZ, GIF,
635
+ JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ. Note that this function
636
+ only supports adding pictures placed over the cells currently, and
637
+ doesn't support adding pictures placed in cells or creating the Kingsoft
638
+ WPS Office embedded image cells
639
+
640
+ Args:
641
+ sheet (str): The worksheet name
642
+ extension (str): The image extension
643
+ picture (Picture): The picture options
644
+
645
+ Returns:
646
+ Optional[Exception]: Returns None if no error occurred,
647
+ otherwise returns an Exception with the message.
648
+ """
649
+ lib .AddPictureFromBytes .restype = c_char_p
650
+ err = lib .AddPictureFromBytes (
651
+ self .file_index ,
652
+ sheet .encode (ENCODE ),
653
+ cell .encode (ENCODE ),
654
+ byref (py_value_to_c (picture , types_go ._Picture ())),
655
+ ).decode (ENCODE )
656
+ return None if err == "" else Exception (err )
657
+
619
658
def add_pivot_table (self , opts : Optional [PivotTableOptions ]) -> Optional [Exception ]:
620
659
"""
621
660
Add pivot table by given pivot table options. Note that the same fields
@@ -880,6 +919,26 @@ def add_table(self, sheet: str, table: Table) -> Optional[Exception]:
880
919
).decode (ENCODE )
881
920
return None if err == "" else Exception (err )
882
921
922
+ def add_vba_project (self , file : bytes ) -> Optional [Exception ]:
923
+ """
924
+ Add vbaProject.bin file which contains functions and/or macros. The file
925
+ extension should be XLSM or XLTM.
926
+
927
+ Args:
928
+ file (bytes): The contents buffer of the file
929
+
930
+ Returns:
931
+ Optional[Exception]: Returns None if no error occurred,
932
+ otherwise returns an Exception with the message.
933
+ """
934
+ lib .AddVBAProject .restype = c_char_p
935
+ err = lib .AddVBAProject (
936
+ self .file_index ,
937
+ cast (file , POINTER (c_ubyte )),
938
+ len (file ),
939
+ ).decode (ENCODE )
940
+ return None if err == "" else Exception (err )
941
+
883
942
def auto_filter (
884
943
self ,
885
944
sheet : str ,
@@ -1572,6 +1631,64 @@ def set_cell_value(
1572
1631
).decode (ENCODE )
1573
1632
return None if err == "" else Exception (err )
1574
1633
1634
+ def set_col_outline_level (
1635
+ self , sheet : str , col : str , level : int
1636
+ ) -> Optional [Exception ]:
1637
+ """
1638
+ Set outline level of a single column by given worksheet name and column
1639
+ name.
1640
+
1641
+ Args:
1642
+ sheet (str): The worksheet name
1643
+ col (str): The column name
1644
+ level (int): The out level, acceptable value from 1 to 7
1645
+
1646
+ Returns:
1647
+ Optional[Exception]: Returns None if no error occurred,
1648
+ otherwise returns an Exception with the message.
1649
+
1650
+ Example:
1651
+ For example, set outline level of column D in Sheet1 to 2:
1652
+
1653
+ .. code-block:: python
1654
+
1655
+ err = f.set_col_outline_level("Sheet1", "D", 2)
1656
+ """
1657
+ lib .SetColOutlineLevel .restype = c_char_p
1658
+ err = lib .SetColOutlineLevel (
1659
+ self .file_index , sheet .encode (ENCODE ), col .encode (ENCODE ), level
1660
+ ).decode (ENCODE )
1661
+ return None if err == "" else Exception (err )
1662
+
1663
+ def set_defined_name (self , defined_name : DefinedName ) -> Optional [Exception ]:
1664
+ """
1665
+ Set the defined names of the workbook or worksheet. If not specified
1666
+ scope, the default scope is workbook.
1667
+
1668
+ Args:
1669
+ defined_name (DefinedName): The defined name options
1670
+
1671
+ Returns:
1672
+ Optional[Exception]: Returns None if no error occurred,
1673
+ otherwise returns an Exception with the message.
1674
+
1675
+ Example:
1676
+ For example, create a table of A1:D5 on Sheet1:
1677
+
1678
+ .. code-block:: python
1679
+
1680
+ err = f.set_defined_name(excelize.DefinedName(
1681
+ name="Amount",
1682
+ refers_to="Sheet1!$A$2:$D$5",
1683
+ comment="defined name comment",
1684
+ scope="Sheet2",
1685
+ ))
1686
+ """
1687
+ lib .SetDefinedName .restype = c_char_p
1688
+ options = py_value_to_c (defined_name , types_go ._DefinedName ())
1689
+ err = lib .SetDefinedName (self .file_index , byref (options )).decode (ENCODE )
1690
+ return None if err == "" else Exception (err )
1691
+
1575
1692
def set_sheet_background (self , sheet : str , picture : str ) -> Optional [Exception ]:
1576
1693
"""
1577
1694
Set background picture by given worksheet name and file path. Supported
0 commit comments