简体   繁体   English

使用 Python 和 openpyxl 将 1 个特定行从一个 Excel 电子表格复制到另一个

[英]Using Python & openpyxl to copy 1 specific row from one excel spreadsheet to another

I'm trying to take 1 row from a worksheet in Excel workbook 'A' and paste it to a specific row in Excel workbook 'B'.我正在尝试从 Excel 工作簿“A”中的工作表中取出 1 行并将其粘贴到 Excel 工作簿“B”中的特定行。 I've gotten this far but I'm stuck on the pasting process:我已经走了这么远,但我坚持粘贴过程:

import openpyxl 

#Open source & destination files
survey_results = "Survey.xlsx"
wb1 = openpyxl.load_workbook(survey_results)
form1 = wb1.worksheets[0]
row2 = form1[2]

backend = "analysis.xlsx"
wb2= openpyxl.load_workbook(backend)
input_sheet = wb2['Input']
row4 = input_sheet[4]


#Copy contents
for i in row2:
    for j in row4:
        input_sheet.cell(row = 4, column = j).value = i.value



#Save to new file
wb2.save('output.xlsx')
import openpyxl 

#Open source & destination files
survey_results = "Survey.xlsx"
wb1 = openpyxl.load_workbook(survey_results)
ws1 = wb1.worksheets[0]

backend = "analysis.xlsx"
wb2 = openpyxl.load_workbook(backend)
input_sheet = wb2['Input']

# Copy contents
for c in range(1,ws1.max_column+1):
    input_sheet.cell(4,c).value = ws1.cell(2,c).value

wb2.save("output.xlsx")    

暂无
暂无

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

相关问题 在 Python 中使用 openpyxl 将行插入 Excel 电子表格 - Insert row into Excel spreadsheet using openpyxl in Python Python Excel 将单元格样式从一个复制到另一个openpyxl - Python Excel copy cell style from one to another openpyxl 如何使用 openpyxl 库将列从一张 excel 表复制到另一张 python? - How can I copy columns from one excel sheet to another with python using openpyxl Library? Openpyxl:代码无法将数据从一个 Excel 电子表格写入另一个 - Openpyxl: Code fails to write data from one Excel spreadsheet to another 使用 Python 从 Excel 电子表格中的特定单元格复制第五个值 - Copy the fifth value from a specific cell in an Excel spreadsheet using Python 如何在openpyxl中将图像从一个Excel复制到另一个? - How to copy image from one excel to another in openpyxl? python 中的 Openpyxl 进行循环以将许多 excel 文件中的特定行合并为一个 - Openpyxl in python making for loop to merge specific row from many excel files into one 如何使用 openpyxl 将工作表从一个工作簿复制到另一个工作簿? - How to copy worksheet from one workbook to another one using openpyxl? 使用 python 中的 openpyxl 将特定行和列添加到另一个 excel 文件中 - add specific rows and column into another excel file using openpyxl in python 如何使用python中的openpyxl将范围从一张纸复制到另一张纸作为值 - How to copy a range from one sheet to another as values using openpyxl in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM