简体   繁体   English

尝试使用 OPENPYXL 将一系列单元格从一个工作簿复制到另一个工作簿

[英]Trying to copy a range of cells from one workbook to another using OPENPYXL

i am fairly new to Python and am trying to copy a selected range of cells (for instance A4:A66) from one Workbook to another.我对 Python 相当陌生,并且正在尝试将选定范围的单元格(例如 A4:A66)从一个工作簿复制到另一个工作簿。 I have managed to take the range i need but am having trouble copying it into another workbook.我已经设法获取了我需要的范围,但无法将其复制到另一个工作簿中。 The code is as follows:代码如下:

import openpyxl as xl;

#Open source workbook
filename = r'C:\Users\FileDestination\SOURCE.xlsx'
wb1 = xl.load_workbook(filename)
ws1 = wb1.worksheets[0]

#Open destination workbook
filename1 = r'C:\Users\FileDestination\Destination.xlsx'
wb2 = xl.load_workbook(filename1)
ws2 = wb2.active

#Read the maximum number of columns and rows in source
mr = ws1.max_row
mc = ws1.max_column

#Copying source range values from source to destination
for r in ws1['B16':'B31']:
    for c in r:
        print(c.value) #Just to see the range selected
        ws2.cell(row = 1, column = 1).value = c.value #I don't believe this is correct as it doesn't work

wb2.save(str(filename1)) #Saving the new workbook

print("Range successfully copied to new Workbook")

The last part of this code is what is perplexing me... I am unsure how i should copy the range, the above code just copies the last line in the range in the first row&column on new Workbook.这段代码的最后一部分让我感到困惑......我不确定我应该如何复制范围,上面的代码只是复制新工作簿第一行和列中范围的最后一行。 Also, i know it is pulling the range i want, just not sure how to copy.另外,我知道它正在拉动我想要的范围,只是不确定如何复制。

Any help would be much appreciated, and thank you任何帮助将不胜感激,谢谢

See this看到这个

x = 1
#Copying source range values from source to destination
for r in ws1['B16':'B31']:
    for c in r:
        print(c.value) #Just to see the range selected
        ws2.cell(row = x, column = 1).value = c.value
        x += 1

暂无
暂无

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

相关问题 Openpyxl - 将单元格范围(带公式)从工作簿复制到另一个 - Openpyxl - Copy range of cells(with formula) from a workbook to another 如何使用 openpyxl 将工作表从一个工作簿复制到另一个工作簿? - How to copy worksheet from one workbook to another one using openpyxl? 想要使用 openpyxl 将工作表从一个工作簿添加到另一个工作簿 - want to add worksheet from one workbook in to another workbook using openpyxl 使用openpyxl将单元格值列从一个工作簿复制到另一个工作簿 - Copy column of cell values from one workbook to another with openpyxl 保存时使用openpyxl从一个工作簿复制到另一个工作簿会导致错误 - Using openpyxl to copy from one workbook to another results in error when saving 使用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 使用 Openpyxl 将特定列从一个工作簿复制到另一个工作簿 - Copying Specific Column from One Workbook to Another using Openpyxl openpyxl - 将值和 styles 从一个范围复制/粘贴到另一个范围 - openpyxl - copy/paste values and styles from one range to another 如何使用python中的openpyxl将范围从一张纸复制到另一张纸作为值 - How to copy a range from one sheet to another as values using openpyxl in python Python/openpyxl - 有没有办法将工作表从一个工作簿复制到另一个具有所有属性的工作簿(精确副本) - Python/openpyxl - Is there a way to copy a worksheet from one workbook to another with all properties (exact copy)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM