jmcnamara / XlsxWriter

A Python module for creating Excel XLSX files.
https://xlsxwriter.readthedocs.io
BSD 2-Clause "Simplified" License
3.66k stars 631 forks source link

New feature: Add signature file for VBA projects #1009

Closed HolgiHo closed 1 year ago

HolgiHo commented 1 year ago

Feature Request

This issue is the same as for the C version of the library: https://github.com/jmcnamara/libxlsxwriter/issues/411

VBA projects can be digitally signed in Excel.

This is an important security feature for many commercial customers: Administrators or users can choose whether to block execution of unsigned VBA macros in their environment, for example, by editing a setting in the Excel Security Center dialog.

For details see the official documentation: https://support.microsoft.com/en-us/office/change-macro-security-settings-in-excel-a97c09d2-c082-46b8-b19f-e8621e8fe373

Technically, the VBA signature file is named vbaProjectSignature.bin and is located in the same folder as vbaProject.bin. Some small additional changes are required in the xlsm file data file to make Excel accept the digital signature.

Digital signatures are added in VBA code editor window, menu Tools -> Digital Signatures. For details, again see the official documentation: https://support.microsoft.com/en-us/office/digitally-sign-your-vba-macro-project-956e9cc8-bbf6-4365-8bfa-98505ecd1c01

This feature was already requested in 2015 in the Python version of the library, see https://github.com/jmcnamara/XlsxWriter/issues/283. However, the PR never get merged as it missed the required contribution standards.

I have extended Xlsxwriter to support adding a VBA project digital signature file, including a functional test, and documentation.

Here is an example for the Python library. Note the new public method workbook.add_signed_vba_project. This is the only API extension I made.

workbook = Workbook("macro_signed.xlsm")
worksheet = workbook.add_worksheet("Foo")

workbook.add_signed_vba_project("./vbaProject.bin", "./vbaProjectSignature.bin")

I will create the corresponding PR soon and reference this issue.

jmcnamara commented 1 year ago

Feature merged to main.

jmcnamara commented 1 year ago

Upstream in version v3.1.5.