简体   繁体   English

Python在Excel中将一个工作表复制到另一个工作表

[英]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.

相关问题 将 excel 工作表复制到另一个工作表或仅使用 python 删除其行 - copying a excel sheet to another or only deleting its rows using python Python Pandas - 从一张纸复制数据并附加到另一张纸的末尾 - Python Pandas - copying data from one sheet and appending at the end of another 从一个工作表复制数据,然后将其粘贴到另一个Excel工作表时,Openpyxl边界线中断 - Openpyxl border line breaks when copying data from one sheet and then pasting it to another excel sheet 使用 python 将段从一个 Excel 文件复制到另一个:xlrd 和 xlsxwriter - Copying the segment from one Excel file to another with python: xlrd and xlsxwriter 从一个 Google 表格复制和粘贴到另一个 - Copying and Pasting from one Google Sheet to another 在Python中将excel工作表从一个工作表复制到另一个工作表 - Copy excel sheet from one worksheet to another in Python 使用Python(和DataNitro)将单元格从一个Excel工作簿中的特定工作表复制到另一个Excel工作簿中的特定工作表 - Using Python (and DataNitro) to copy cells from a particular sheet in one Excel workbook, to a particular sheet in another Excel workbook 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 将数据从一个 Excel 工作表复制到另一个 Excel 工作表 - Copy data from one excel sheet to another excel sheet 使用 Python 将单元格范围从 Excel 复制到另一个 - Copying range of cells from on Excel to another with Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM