6

I'm writing a program that, summarized, takes a notepad file and saves it as a excel file.

Right now my program opens up a blank excel file I have created, just "Book1.xls":

xlApp = Dispatch("Excel.Application")
xlApp.Visible=0
xlWb = xlApp.Workbooks.Open(file_path+"/Book1.xls")
workBook = xlApp.ActiveWorkbook
sheet = xlApp.ActiveSheet

and it uses Book1.xls to write into and format as is required, then saves it as another file name using

workBook.SaveAs(new_file_path+'/UpdatedSheet.xls')

I want to know how to simply create a new excel file to write to, then save as a file. Without the need of having Book1.xls already created in a specific directory.

1 Answer 1

10

You can create a new workbook using the Add method of the Workbooks object:

>>> import win32com.client as win32
>>> excel = win32.Dispatch("Excel.Application")
>>> workbook = excel.Workbooks.Add()
>>> workbook.SaveAs(new_file_path+'/UpdatedSheet.xls')
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.