2

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?

1 Answer 1

3

I think I have figured out an alternative to this.
First I created an xlsm file with the VBA code to format each of the sheets within each of my workbooks in a file folder. Then in my python script I will first create all of the excel files that I want to create without any formatting, then trigger python to call the Macro in this xlsm file to run through all of the workbooks and each of the sheets to format each them in the fashion that I want and by using Excel's formatting instead of python's excel formatting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.