0

I have the below error calling a macro via python using xlwings

"pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352562), None)"

My full code: it opens the latest CSV file in downloads folder and runs macro on it

import glob
import os
from subprocess import Popen
import os, os.path


list_of_files = glob.glob('C:/Users/Martina/Downloads/*.csv')
latest_csv_file = max(list_of_files, key=os.path.getctime)
print(latest_csv_file)
file_name =latest_csv_file
os.startfile(file_name)

import xlwings as xw

wb = xw.Book(r'C:\Work\Automation\Resources\Personal All MAcro codes.xlsb')
your_macro = wb.macro('Iwebsite')
your_macro(1,2)
Martina
  • 43
  • 7

1 Answers1

0

I found my answer in that link:Calling a macro to a different workbook python

My code to solve my problem:

import glob
import os
from subprocess import Popen
import os, os.path


list_of_files = glob.glob('C:/Users/Martina/Downloads/*.csv')
latest_csv_file = max(list_of_files, key=os.path.getctime)
print(latest_csv_file)
file_name =latest_csv_file
os.startfile(file_name)

import xlwings as xw

wb = xw.Book(r'C:\Work\Automation\Resources\Personal All MAcro codes.xlsb')
your_macro = wb.macro('Iwebsite')
your_macro(1,2)
def python_macro(wb_path, app):
    # Optionally verify that files are all xls*, csv, or directory files

    wb = app.books.open(wb_path)
    sheet = wb.sheets.active

    # Rewrite macro in python+xlwings below
    print(sheet.range('A1').value)


with xw.App() as app:
    for arg in sys.argv[1:]:
        path = join(os.getcwd(), arg)

        if os.path.isdir(path):
            # Optionally process further subdirectories

            files = [f for f in listdir(path) if isfile(join(path, f))]

            for file in files:
                python_macro(file, app)

        elif os.path.isfile(path):
            python_macro(path, app)
Martina
  • 43
  • 7