Working with an unsaved excel workbook in python

Viewed 90

I am exporting excel documents out of another program and need to rename them under a different name, I have gotten to the point where I can pull the open excel workbook into my code, as you can see below, but I can't figure out how to rename the workbook to foo.xlxs going this route. What is my most optimal way to attack this?

from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")

xlApp.Visible=1

name = xlApp.ActiveWorkbook.Name

print(name)

Output:

activeworkbook.xlsx
1 Answers

This is possible solution.

import win32com.client

excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Add()

#what you do to your workbook, add value to sheets

workbook.SaveAs(workbook_full_dir)
workbook.Close(True)
Related