简体   繁体   中英

How to customize the action of a button in excel sheet using xlsxwriter

I am wondering to use xlsxwriter to control my excel sheet instead of using old fashioned way by using VBA. I need a button to trigger some actions. What I read from the official documentation is

workbook.add_vba_project('./vbaProject.bin')
worksheet.insert_button('B3', {'macro':   'say_hello',
                               'caption': 'Press Me'})

I don't know how to define my own function as a macro and I don't know how to generate "vbaProject.bin". Is there anyway to write a macro in format like python function and directly assign it to the button?

If I must include the macro in vbaProject.bin, how can I do that? Hope it is not something like vba.

So you have many ways to control and automate your files:

  1. You can use vba

  2. You can use xlsxwriter which uses python syntax

  3. You can use a library called PyXLL https://www.pyxll.com/docs/userguide/macros.html . It seems you are able to write a macro using python syntax but i haven't tried it, you could have a look as it might be what you are looking for.

About your question on how to inject a macro to your excel using xlsxwriter have a look at my answer here, i think it is quite detailed:

Add dataframe and button to same sheet with XlsxWriter

By the way vba is not hard to learn and there is a lot of information online. If you don;t want to write the code manually you can record the macro by pressing record, do your thing and stop the recording. Then you will have the code which you can inject it to your file (look at my tutorial above)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM