繁体   English   中英

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

[英]Copy column to another sheet in Python

我一直试图通过openpyxl将可变长度列复制到另一张表。 我要做的是复制,例如,从第2 row = sheet.max_row到第2行,直到row = sheet.max_row ,并将其粘贴到同一工作簿中的另一个工作表中。 指定它将开始粘贴的工作表中的第一个单元格也会很好。

我尝试过本教程 (将单元格区域复制并粘贴到另一个工作簿中)无济于事。

到目前为止,我的代码设置如下:

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

有没有人有任何可以提供帮助的代码? 如果没有,可以查看哪些资源以获取有关如何解决此问题的信息?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM