[英]Python copying one work sheet to another in excel
I need to copy one excel worksheet to another. 我需要将一个Excel工作表复制到另一个。 What I found so far is FillAcrossSheets to do this operation in Python. 到目前为止,我发现的是FillAcrossSheets在Python中执行此操作。 Below is my code: 下面是我的代码:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = False #Keep the excel sheet closed
excel.DisplayAlerts = False #"Do you want to over write it?" Will not Pop up
filepath = "C:/Pwr_Mgmt_Template.xlsx"
#Copy data from sheet named Template
sheetID = 'Template'
#Open the excel sheet
wb = excel.Workbooks.Open(filepath)
ws = wb.Worksheets(sheetID)
#Create a new sheet with name SATA-SkewData_Core1.1
ws = wb.Worksheets.Add()
ws.Name = "SATA-SkewData_Core1.1"
ws = wb.Worksheets(sheetID)
#Copy contents from Template to SATA-SkewData_Core1.1
wb.Worksheets.FillAcrossSheets(wb.Worksheets(sheetID).Range("A5:AF16"))
wb.Save()
excel.Application.Quit()
Now, if I need to create other sheets then Fill Across Sheets method will copy to these sheets too and will destroy the data. 现在,如果我需要创建其他工作表,则“跨表填充”方法也将复制到这些工作表中,并销毁数据。 Rather than filling all across the sheets, is there a way to selectively copy from one sheet to exactly another sheet? 有没有办法选择性地从一张纸复印到另一张纸,而不是全部填满纸?
In standard procedure I opened two worksheets from two different workbooks. 在标准过程中,我从两个不同的工作簿中打开了两个工作表。 The below line copies the template worksheet (ws_temp) to the power measurement work sheet (ws_pwr). 下一行将模板工作表(ws_temp)复制到功率测量工作表(ws_pwr)。 The row number can also be supplied as an argument so that the same template can be copied in the same worksheet but with a different row number: 行号也可以作为参数提供,以便可以将相同的模板复制到同一工作表中,但行号不同:
ws_temp.Range("A5:AF16").Copy(ws_pwr.Range("A%s:AF%s" % (chart_first_row, chart_last_row)))
The row numbers copied from the template are 5 and 16 respectively. 从模板复制的行号分别为5和16。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.