正在运行宏个人.XLSB使用python

2024-03-29 10:02:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在个人.XLSB使用python。在

当我自己从excel工作簿运行宏PERSONAL.XLSB!PIVOTS时,它就起作用了。另外,如果我将vba代码复制并粘贴到“This Workbook”并运行xlApp.Run('Pivots')它就可以工作了。在

但是,当我使用xlApp.Run('C:\\Users\\AppData\\Roaming\\Microsoft\\Excel\\XLSTART\\PERSONAL.XLSB!Pivots')时,它将不起作用。我需要它跑进去个人.XLSB'因为我将在多个文件中使用相同的宏。在

from __future__ import print_function
import unittest
import os.path
import win32com.client

class ExcelMacro(unittest.TestCase):
    def test_excel_macro(self):
        try:
            xlApp = win32com.client.DispatchEx('Excel.Application')
            xlsPath = os.path.expanduser('C:\\Users\\colm_mcsweeney\\Documents\\Attachments\\Dash.09.05.19.xlsm')
            wb = xlApp.Workbooks.Open(Filename=xlsPath)
            xlApp.Visible = True
            xlApp.Run("C:\\Users\\AppData\\Roaming\\Microsoft\\Excel\\XLSTART\\PERSONAL.XLSB!Pivots")
            wb.Save()
            xlApp.Quit()
            print("Macro ran successfully!")
        except:
            print("Error found while running the excel macro!")
            xlApp.Quit()
if __name__ == "__main__":
    unittest.main()

Tags: runimportunittestexcelroamingusersappdatapersonal