1####################################################################### 2# 3# An example of adding macros to an XlsxWriter file using a VBA project 4# file extracted from an existing Excel xlsm file. 5# 6# The vba_extract.py utility supplied with XlsxWriter can be used to extract 7# the vbaProject.bin file. 8# 9# An embedded macro is connected to a form button on the worksheet. 10# 11# Copyright 2013-2021, John McNamara, jmcnamara@cpan.org 12# 13import xlsxwriter 14 15# Note the file extension should be .xlsm. 16workbook = xlsxwriter.Workbook('macros.xlsm') 17worksheet = workbook.add_worksheet() 18 19worksheet.set_column('A:A', 30) 20 21# Add the VBA project binary. 22workbook.add_vba_project('./vbaProject.bin') 23 24# Show text for the end user. 25worksheet.write('A3', 'Press the button to say hello.') 26 27# Add a button tied to a macro in the VBA project. 28worksheet.insert_button('B3', {'macro': 'say_hello', 29 'caption': 'Press Me', 30 'width': 80, 31 'height': 30}) 32 33workbook.close() 34