简体   繁体   English

如何使用 openpyxl 库将列从一张 excel 表复制到另一张 python?

[英]How can I copy columns from one excel sheet to another with python using openpyxl Library?

I am new to programming.我是编程新手。 I am trying to copy columns from one excel sheet to another but I'm having problem with my code as only the last column data is being copied to the destination file.我正在尝试将列从一张 excel 工作表复制到另一张,但我的代码有问题,因为只有最后一列数据被复制到目标文件。 I've attached the code and the result i kept getting.我附上了代码和我不断得到的结果。

Jan_2020 ="C:\\Users\\yaxee\\OneDrive\\Desktop\\NBET 
Extraction data\\JANUARY 2020.xlsx"
wb1 = xl.load_workbook(Jan_2020)
ws1 = wb1.worksheets[0]

Extraction_WB ="C:\\Users\\yaxee\\OneDrive\\Desktop\\ExtractionWB.xlsx"
wb2 = xl.load_workbook(Extraction_WB)
ws2 = wb2.worksheets[0]

#copy from wb1
for i in range(4, 34):
    c = ws1.cell(row = i, column = 4)
#paste in ws2
for j in range(3, 33):
    ws2.cell(row = j, column = 4).value = c.value
wb2.save(str(Extraction_WB))

Source sheet:源表: 源表

Destination sheet:目的地表: 在此处输入图像描述

If I understand your need, you need to put the second loop inside the first loop for copy the whole range of rows.如果我理解您的需要,您需要将第二个循环放在第一个循环中以复制整个行范围。 Would this work?这行得通吗? Kr.

#copy from wb1
for i in range(4, 34):
    c = ws1.cell(row = i, column = 4)
    #paste in ws2
    for j in range(3, 33):
        ws2.cell(row = j, column = 4).value = c.value
wb2.save(str(Extraction_WB))

You don't need two loops.你不需要两个循环。 You are just constantly overriding the value of c until it has the last value of the column.您只是不断地覆盖c的值,直到它具有该列的最后一个值。

Then, you simply write that value to all rows of the second sheet.然后,您只需将该值写入第二张工作表的所有行。

Instead, use one loop, to read a single value from the current row, and write it to the corresponding row in the second sheet:相反,使用一个循环,从当前行读取单个值,并将其写入第二张工作表中的相应行:

for row in range(4, 34):
    #copy from wb1
    c = ws1.cell(row=row, column=4)
    #paste in ws2
    ws2.cell(row=row-1, column=4, value=c.value)

If you have some general transformation between the sheets, a more general approach as follows:如果您在工作表之间有一些通用的转换,则更通用的方法如下:

step = 2
read_start_row = 4
write_start_row = 3
amount_of_rows = 30

for i in range(0, amount_of_rows, step):
    #copy from wb1
    c = ws1.cell(row=read_start_row+i, column=4)
    #paste in ws2
    ws2.cell(row=write_start_row+(i/step), column=4, value=c.value)

暂无
暂无

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

相关问题 如何使用python中的openpyxl将范围从一张纸复制到另一张纸作为值 - How to copy a range from one sheet to another as values using openpyxl in python 使用 python 将列从一个 excel 文件复制到另一个 excel 文件表 - Copy columns from one excel file to another excel file sheet using python 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 如何在跳过空单元格的同时将数据从一张纸复制到另一张纸 - Python 和 Openpyxl - How to copy data from one sheet to another while skipping empty cells - Python and Openpyxl Python Excel 将单元格样式从一个复制到另一个openpyxl - Python Excel copy cell style from one to another openpyxl 使用 Python 和 openpyxl 将 1 个特定行从一个 Excel 电子表格复制到另一个 - Using Python & openpyxl to copy 1 specific row from one excel spreadsheet to another 使用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中将图像从一个Excel复制到另一个? - How to copy image from one excel to another in openpyxl? 如何使用 openpyxl 将 csv 文件写入 Excel 工作表? - How can i write a csv file to an excel sheet using openpyxl? 如何使用 Python 和 openpyxl 替换 Excel 工作表中的 HTML 代码? - How can I replace HTML code inside an excel sheet using Python and openpyxl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM