使用Python xlsxwriter在工作簿中运行VBA宏

时间:2018-09-28 09:23:52

标签: python excel vba xlsxwriter

我是Python初学者,请耐心等待。

我想知道有没有一种方法可以在我关闭它之前在打开的工作簿中打开并运行宏Excel代码?

这是代码的一部分:

workbook = xlsxwriter.Workbook('Somedata.xlsx')
worksheet = workbook.add_worksheet()

# Write some numbers, with row/column notation.
# "save","identifier", "ISIN" "name" are just lists with data

worksheet.write_column(0, 1, save * number_of_rows)
worksheet.write_column(0, 2, identifier * number_of_rows)
worksheet.write_column(0, 3, ISIN)
worksheet.write_column(0, 4, name)


# Set coulmns width

worksheet.set_column('A:A', 15)
worksheet.set_column('B:C', 4)
worksheet.set_column('D:D', 13)
worksheet.set_column('E:E', 55)


workbook.close()

谢谢:)

1 个答案:

答案 0 :(得分:2)

此链接对此https://xlsxwriter.readthedocs.io/working_with_macros.html进行了解释

您必须解压缩vbaProject.bin文件,然后使用add_vba_project()方法将其添加到XlsxWriter工作簿中:

workbook.add_vba_project('./ vbaProject.bin')