在pywin32中创建一个新的Excel文件 [英] Creating a new Excel File in pywin32

查看:241
本文介绍了在pywin32中创建一个新的Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序经过总结后会使用记事本文件并将其另存为excel文件.

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

现在,我的程序打开了一个我创建的空白excel文件,只是"Book1.xls":

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

,它使用Book1.xls进行写入并按要求进行格式化,然后使用

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

我想知道如何简单地创建一个要写入的新excel文件,然后另存为文件.无需在特定目录中已经创建Book1.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.

推荐答案

您可以使用Workbooks对象的Add方法创建一个新的工作簿:

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

这篇关于在pywin32中创建一个新的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆