Python Forum
Dynamically Copy and paste column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamically Copy and paste column
#1
Hello all,
I am new to the forum and python I am seeking some assistance as I have searched and am not able to find the answer I need. What I am doing is coping a certain column and pasting it into another workbook right now I am specifying how many rows to copy and paste as you can see below in copyrange and pasterange. Is there a way in python to copy and paste based on the amount of rows with data? Ex: column A has data from A2:A10 but next weeks file has data from A2:A14 I have my code below I am using to copy and paste. Any help is appreciated!

# File to be copied
wb = openpyxl.load_workbook(
    "C:/Users/u678153/Desktop/Account_Detail.100219-added MI_210 vFINAL.xlsx")  # Add file name
sheet = wb["Sheet1"]  # Add Sheet name

# File to be pasted into
template = openpyxl.load_workbook("C:/Users/u678153/Desktop/07_19 ITS Backup current.xlsx")  # Add file name
temp_sheet = template["Download"]  # Add Sheet name


# Copy range of cells as a nested list
# Takes: start cell, end cell, and sheet you want to copy from.
def copyRange(startCol, startRow, endCol, endRow, sheet):
    rangeSelected = []
    # Loops through selected Rows
    for i in range(startRow, endRow + 1, 1):
        # Appends the row to a RowSelected list
        rowSelected = []
        for j in range(startCol, endCol + 1, 1):
            rowSelected.append(sheet.cell(row=i, column=j).value)
        # Adds the RowSelected List and nests inside the rangeSelected
        rangeSelected.append(rowSelected)

    return rangeSelected


# Paste range
# Paste data from copyRange into template sheet
def pasteRange(startCol, startRow, endCol, endRow, sheetReceiving, copiedData):
    countRow = 0
    for i in range(startRow, endRow + 1, 1):
        countCol = 0
        for j in range(startCol, endCol + 1, 1):
            sheetReceiving.cell(row=i, column=j).value = copiedData[countRow][countCol]
            countCol += 1
        countRow += 1


print("Processing...")
selectedRange = copyRange(6, 3, 6, 26884, sheet)  # Change the 4 number values
pasteRange(1, 3, 1, 26884, temp_sheet, selectedRange)
# You can save the template as another file to create a new file here too.s
template.save("C:/Users/u678153/Desktop/07_19 ITS Backup current.xlsx")
print("Range copied and pasted!")
Reply


Messages In This Thread
Dynamically Copy and paste column - by Patriot17 - Dec-11-2019, 11:02 PM
RE: Dynamically Copy and paste column - by Larz60+ - Dec-12-2019, 11:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy and Paste Files - Command MicheliBarcello 2 1,015 Jun-25-2024, 05:04 AM
Last Post: rodiongork
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 1,481 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 1,709 Feb-07-2024, 12:24 PM
Last Post: Viento
  What script to paste folders thenewcoder 1 1,447 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 4,277 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  Please help me [copy and paste file from src to dst] midomarc 2 1,959 Nov-24-2022, 10:13 PM
Last Post: midomarc
  Copy a column from one dataframe to another dataframe Led_Zeppelin 17 21,350 Jul-08-2022, 08:40 PM
Last Post: deanhystad
  ImageTk Paste KDog 14 10,884 Jun-27-2021, 11:07 AM
Last Post: KDog
  Copy column from one existing excel file to another file mkujawsk 0 7,998 Apr-14-2021, 06:33 PM
Last Post: mkujawsk
  Cut and Paste Oshadha 3 3,560 Jan-20-2021, 04:27 PM
Last Post: spaceraiders

Forum Jump:

User Panel Messages

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