Reputation: 153
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.
Upvotes: 0
Views: 1753
Reputation: 21
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')
Upvotes: 1
Reputation: 161
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
Upvotes: 0