Python Forum
openpyxl - How can I copy some row from an excel file and paste them in another one?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
openpyxl - How can I copy some row from an excel file and paste them in another one?
from a source excel file I have to copy the rows (with theirs formatting) where I find a specific string placed in a specific column. In few words I wanna implement a filter (see my attached screenshot to have an example).

now, I don't know so much about openpyxl, I chosed it because it seems quite popular. today I started to write a draft of the code, but now I don't know how to complete it. can you give me a hand please?
from io import BytesIO
import openpyxl

def FindColumnID(name, ws):
    header = ws[1]
    for cell in header:
        if cell.value == name:
            return header.index(name) + 1

def CreateDocument(database_path, text_path, column_name):
    text_file = open(text_path, "r")
    text =
    ls = text.split("\n")
    ls.insert(0, column_name)

    database_wb = openpyxl.load_workbook(database_path)
    database_ws = database_wb[database_wb.sheetnames[0]]

    column_id = FindColumnID(column_name, database_ws)

    output = BytesIO()    
    result_wb = openpyxl.Workbook(output)
    result_ws =
    result_ws.title = "RESULT"

    for row in database_ws.rows:
        for element in ls:
            if database_ws.cell(row, column_id).value == element:
                # copy the row with the formatting in result_wb  <---------- how can I do it?

Attached Files

Did some quick googling for you. According this openpyxl article, you can use the following code to retrieve values. There is also another way using the columns and rows, which is probably the better the idea. I advise you to read the article. It's a lot of information.

a = sheet["A1"]
b = sheet["D2"]
c = sheet["F7"]
look at this SO
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open an excel file Newbie1114 1 170 Jun-16-2021, 09:11 PM
Last Post: Gribouillis
  ImageTk Paste KDog 13 605 Jun-03-2021, 03:27 AM
Last Post: deanhystad
  Filter Excel and Convert an Excel File giddyhead 0 264 May-13-2021, 06:31 PM
Last Post: giddyhead
  Problems with inserting images into an Excel File FightingFarmer 2 293 May-12-2021, 10:03 PM
Last Post: FightingFarmer
  Copy column from one existing excel file to another file mkujawsk 0 329 Apr-14-2021, 06:33 PM
Last Post: mkujawsk
  Importing text file into excel spreadsheet with formatting david_dsmn 1 329 Apr-05-2021, 10:21 PM
Last Post: david_dsmn
  Add a new column when I extract each sheet in an Excel workbook as a new csv file shantanu97 0 368 Mar-24-2021, 04:56 AM
Last Post: shantanu97
  Need help with saving output into an excel file Beyondfacts 4 550 Mar-22-2021, 11:51 AM
Last Post: jefsummers
  How to append a value to specific excel cell using openpyxl hobbyist 0 592 Mar-05-2021, 07:14 PM
Last Post: hobbyist
  Python Matplotlib: Create chart for every 4 columns in Excel file JaneTan 2 586 Feb-28-2021, 05:02 AM
Last Post: JaneTan

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020