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?
#1
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 = text_file.read()
    text_file.close()
    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 = slu_wb.active
    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?
                #
                #
                break

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
openpyxl - How can I copy some row from an excel file and paste them in another one? - by aquerci - Jun-06-2020, 06:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 384 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Python openyxl not updating Excel file MrBean12 1 451 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 559 Feb-07-2024, 12:24 PM
Last Post: Viento
  What script to paste folders thenewcoder 1 741 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,375 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 975 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 2,282 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  Excel Automation using Openpyxl pradyumnajpn 1 867 May-16-2023, 09:38 AM
Last Post: Larz60+
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,215 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,348 Feb-20-2023, 07:19 PM
Last Post: avd88

Forum Jump:

User Panel Messages

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