簡體   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