samedi 28 février 2015

Python Excel Run Macro to xlsx file

How do I take the Macro from my xslm file and apply it to my xlsx file?



import cx_Oracle
import xlsxwriter
import win32com.client

SQL = "Select * FROM TABLE"
cursor1 = con.cursor()
cursor1.execute(SQL)
workbook = xlsxwriter.Workbook('OUTPUT.xlsx')
worksheet = workbook.add_worksheet('Summary - Attendee')
worksheet.set_tab_color('red')

for i, row in enumerate(cursor1):
for j, col in enumerate(row):
worksheet.write(i+1,j,col)

excel = win32com.client.Dispatch('Excel.Application')
excel.Visible = 1
excel.Workbooks.Open(Filename="Path\Macro.xlsm")
excel.Application.Run("Format")
excel.Workbooks(1).Close(SaveChanges=1)
excel.Application.Quit()
excel = 0

workbook.close()

Macro.xlsm VBA code:
Sub Format()
Sheet1.Select
Cells.Select
Cells.EntireColumn.Autofit
End Sub


Both of these work but as separate processes and files. I want to call the Macro from xlsm and apply it into my xlsx file to format my xlsx file. Any ideas?


Aucun commentaire:

Enregistrer un commentaire