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