File: macros.py

package info (click to toggle)
xlsxwriter 3.1.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 56,308 kB
  • sloc: python: 51,511; javascript: 7,768; sh: 284; makefile: 195; perl: 75
file content (33 lines) | stat: -rw-r--r-- 995 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#######################################################################
#
# An example of adding macros to an XlsxWriter file using a VBA project
# file extracted from an existing Excel xlsm file.
#
# The vba_extract.py utility supplied with XlsxWriter can be used to extract
# the vbaProject.bin file.
#
# An embedded macro is connected to a form button on the worksheet.
#
# SPDX-License-Identifier: BSD-2-Clause
# Copyright 2013-2023, John McNamara, jmcnamara@cpan.org
#
import xlsxwriter

# Note the file extension should be .xlsm.
workbook = xlsxwriter.Workbook("macros.xlsm")
worksheet = workbook.add_worksheet()

worksheet.set_column("A:A", 30)

# Add the VBA project binary.
workbook.add_vba_project("./vbaProject.bin")

# Show text for the end user.
worksheet.write("A3", "Press the button to say hello.")

# Add a button tied to a macro in the VBA project.
worksheet.insert_button(
    "B3", {"macro": "say_hello", "caption": "Press Me", "width": 80, "height": 30}
)

workbook.close()