Spire.XLS for Python 16.8.2 supports macro (VBA) functionality

2026-08-25 07:22:24

We're pleased to announce the release of Spire.XLS for Python 16.8.2. This version supports macro (VBA) functionality, including adding, getting, and deleting VBA projects, as well as configuring PDF encryption when converting Excel to PDF. Meanwhile, an issue that occurred when combining multiple CopyRangeOptions enum values has also been successfully fixed. More details are listed below.

Here is a list of changes made in this release

Category ID Description
New Feature SPIREXLS-6151 Adds support for macro (VBA) functionality, including adding, getting, and deleting VBA projects.
//1.Add
workbook = Workbook()

# Add VBA project to document
vbaProject = workbook.VbaProject
vbaProject.Name = "SampleVBAMacro"
text = "Code page before modification: " + str(vbaProject.CodePage) + "\n"
vbaProject.CodePage = 936  # Set encoding to support Chinese characters

text += "Code page after modification: " + str(vbaProject.CodePage) + "\n"
File.WriteAllText(outputFile_TXT, text)

vbaModule = vbaProject.Modules.Add("SampleModule", VbaModuleType.Module)
# Does not support Chinese characters
vbaModule.SourceCode = """
    Sub ExampleMacro()
        ' Declare variables
        Dim ws As Worksheet
        Dim i As Integer
        ' Set reference to active worksheet
        Set ws = ActiveSheet
        ' Clear worksheet content (optional)
        ws.Cells.Clear
        ' Fill sample data
        With ws
            ' Write header row
            .Range("A1: C1").Value = Array("Serial No.", "Project Name", "Amount")
            ' Loop to fill 10 rows of data
            For i = 1 To 10
                .Cells(i + 1, 1).Value = i  ' Serial number column
                .Cells(i + 1, 2).Value = "Project " & i  ' Project name column
                .Cells(i + 1, 3).Value = i * 100  ' Amount column (sample calculation)
            Next i
            ' Auto-fit column widths
            .Columns("A:C").AutoFit
            ' Format header row
            With.Range("A1:C1")
                .Font.Bold = True
                .Interior.Color = RGB(200, 220, 255)  ' Light blue background
            End With
            ' Format amount column
            .Range("C2:C11").NumberFormat = "$#,##0.00"
        End With
        ' Show completion message
        MsgBox "Data population complete!", vbInformation, "Operation Prompt"
    End Sub"""

workbook.SaveToFile(outputFile_Xls, FileFormat.Version97to2003)
workbook.SaveToFile(outputFile_Xlsb, FileFormat.Xlsb2007)
workbook.SaveToFile(outputFile_Xlsm, FileFormat.Xlsm)
workbook.SaveToFile(outputFile_Xltm, FileFormat.XLTM)
workbook.Dispose()

//2.Obtain and modify
workbook = Workbook()
workbook.LoadFromFile(inputFile)  

worksheet = workbook.Worksheets[0]
vbaProject = workbook.VbaProject

// Modify worksheet module
mod = vbaProject.Modules.GetWorksheetModule(worksheet)
mod.Name = "IVbaModule"
mod.SourceCode = "Dim lRow As Long"
mod.Type = VbaModuleType.Module

// Save file
workbook.SaveToFile(outputFile)
workbook.Dispose()

//3. Delete
workbook1 = Workbook()
workbook1.LoadFromFile(inputFile_Module)
vbaProject1 = workbook1.VbaProject
vbaProject1.Modules.Remove("SampleModule")
workbook1.SaveToFile(outputFile_1)
workbook1.Dispose()
New Feature SPIREXLS-6169 Adds support for configuring PDF encryption when converting Excel to PDF.
            workbook = Workbook()
            #Load a excel document
            workbook.LoadFromFile(inputFile)   
            #Set permission password and permissions to encrypt converted pdf
            workbook.ConverterSetting.PdfSecurity.Encrypt("123", PdfPermissionsFlags.Print)
            #Convert excel to pdf
            workbook.SaveToFile(outputFile, FileFormat.PDF)
            workbook.Dispose()
Bug Fix SPIREXLS-6199 Fixes the issue where combining multiple CopyRangeOptions enum values threw an error.
Click the link below to download Spire.XLS for Python 16.8.2: