简体   繁体   English

将列复制到Python中的另一个工作表

[英]Copy column to another sheet in Python

I've been trying to copy a variable length column to another sheet through openpyxl. 我一直试图通过openpyxl将可变长度列复制到另一张表。 What I'm looking to do is copy, for example, column B from row 2 up to row = sheet.max_row and paste it into another sheet within the same workbook. 我要做的是复制,例如,从第2 row = sheet.max_row到第2行,直到row = sheet.max_row ,并将其粘贴到同一工作簿中的另一个工作表中。 Specifying the first cell in the sheet in which it will start pasting in would be nice too. 指定它将开始粘贴的工作表中的第一个单元格也会很好。

I've tried following this tutorial (copy and paste cell ranges into another workbook) to no avail. 我尝试过本教程 (将单元格区域复制并粘贴到另一个工作簿中)无济于事。

So far I have my code set up like this: 到目前为止,我的代码设置如下:

import openpyxl
wb = openpyxl.load_workbook('workbook1.xlsx')
wb.create_sheet('sheet2') # this is where I want the cells to be pasted into
sheet = wb['sheet1'] # name of the sheet that is being analyzed


wb.save('workbook1.xlsx') # 

Does anyone have any code that could help? 有没有人有任何可以提供帮助的代码? If not, what resources are available to look at for information on how to solve this problem? 如果没有,可以查看哪些资源以获取有关如何解决此问题的信息?

ws1 = wb.active # source
ws2 = wb['sheet2'] # destination

for cell in ws1['B:B']: #column B
    print('Printing from ' + str(cell.column) + str(cell.row))
    ws2.cell(row = cell.row, column = 1, value = cell.value)

wb.save('workbook1.xlsx')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python中将excel工作表从一个工作表复制到另一个工作表 - Copy excel sheet from one worksheet to another in Python 使用 Python 使用 Google API 将电子表格工作表复制到另一个工作表 - Copy spreadsheet sheet to a another one with Google API using Python 如何在 Python 中将 Excel 工作表复制到另一个工作簿 - How to copy over an Excel sheet to another workbook in Python 如何使用python将Excel工作表复制到具有相同格式的另一个工作簿 - How to copy excel sheet to another workbook with same format using python 使用 Python 在另一个已创建的文件中复制 excel 表和样式 - Copy excel sheet with style in another already-created file with Python (Python)如何将列复制并粘贴到另一个表? - (Python) How to Copy and Paste a column to another table? 有没有办法将特定列从 Excel 工作表(例如 sheet_1)复制到 sheet_2 中的另一列? 使用 Python - Is there any way to copy a particular column from excel sheet (say sheet_1) to the other column which is in sheet_2? Using 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 Pandas在不更改任何数据的情况下将列从一张纸复制到另一张纸? - Python Pandas copy columns from one sheet to another sheet without changing any data? 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM