Creating a new Excel File in pywin32

可紊 提交于 2019-12-10 18:34:39

问题


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:


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')


来源:https://stackoverflow.com/questions/11213045/creating-a-new-excel-file-in-pywin32

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!