0

I have a basic requirement.

I have a .csv file and a .xlsm file which has a macro. I need to copy the .csv file's contents to that .xlsm file and run the macro. I need to use Python.

Is there any script possible to accomplish this?

Thanks in advance.

vasanthfriend
  • 143
  • 1
  • 1
  • 7
  • Hello and welcome to StackOverflow. Please show us what you have tried already, so that we can help you further. – codeling Jan 20 '14 at 15:10

2 Answers2

1
file_dir = os.path.dirname(import_file_path) 
filename = 'sample.xlsx' 
writer = pd.ExcelWriter(filename, engine='xlsxwriter')#Write excel file after write data.(only for write temporary data.)
df.to_excel(writer, sheet_name='ResultSet', index=False)
# ATTACH MACRO AND CREATE .XLSM FILE#
filename_macro =  'sample.xlsm'#Create Macro File.
workbook = writer.book
workbook.filename = filename_macro
workbook.add_vba_project('vbaProject.bin')
writer.save()
if os.path.exists("sample.xlsm"):#Check file
    xl = win32com.client.Dispatch("Excel.Application")
    xl.Workbooks.Open(os.path.abspath("sample.xlsm"))
   xl.Application.Quit() # Comment this out if your excel script closes
    del xl


    if os.path.exists( 'ResultSet1.xlsx'):#checked file exit or not
        # Make duplicate file in user selected directory
        xl1 = load_workbook(filename= 'ResultSet1.xlsx')
        xl1.save('ResultSet.xlsx')
0

this link should help to open XLSM via python an fill it with data... Python, Excel, and Charts using win32com

this one shows you how to run a macro Python - run Excel macro

Community
  • 1
  • 1
Ernst
  • 161
  • 8