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
#2
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"]
Reply
#3
look at this SO https://stackoverflow.com/q/23332259/4046632
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

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 249 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 346 Feb-07-2024, 12:24 PM
Last Post: Viento
  What script to paste folders thenewcoder 1 637 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 752 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 1,923 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  Excel Automation using Openpyxl pradyumnajpn 1 771 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,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 2,839 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 770 Feb-16-2023, 08:11 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

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