Python Forum
How to Copy Single Value From One Excel Sheet to Another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Copy Single Value From One Excel Sheet to Another
#5
Thank You! I will check this out.

(Dec-29-2020, 09:28 AM)Pedroski55 Wrote: Not too sure exactly what you want.

I don't have to copy any formulas or charts, so I never tried and I don't know about that.

I have to update my attendance + scores timetable each week.

This is the first step, may be that will help. It copies all cells in each sheet, after column 9, to a target file.

def copyOldData():
    
    print(f'Opening the file attendanceSummer2020_{clas}latestPyMade.xlsx ....')
    pathToExcel = f'/home/pedro/attendanceSummer2020/{clas}/'
    attendanceFile = f'attendanceSummer2020_{clas}latestPyMade.xlsx'
    target = f'attendanceSummer2020_First9colsOnly{clas}.xlsx'
    saveFileName = attendanceFile.split('.')
    outputFile = saveFileName[0] + '_Step1.xlsx'

    sourceFile = openpyxl.load_workbook(pathToExcel + attendanceFile)
    sourceSheetNames = sourceFile.sheetnames

    targetFile = openpyxl.load_workbook(pathToExcel + target)
    targetSheetNames = targetFile.sheetnames

    # this inserts the maximum attendance value in the target file

    for sheet in sourceSheetNames:
        sourceFileActiveSheet = sourceFile[sheet]
        targetFileActiveSheet = targetFile[sheet]
        targetFileActiveSheet.cell(row=1, column=5, value='max Attendance')
        maxAttendance = sourceFileActiveSheet.cell(row=2, column=5).value # get the value
        targetFileActiveSheet.cell(row=2, column=5, value=maxAttendance) # write the value


           
    # copy all data after column 9 to the target file
    # move everything up 3 columns
    
    for sheet in sourceSheetNames:
        sourceFileActiveSheet = sourceFile[sheet]
        targetFileActiveSheet = targetFile[sheet]
        maxRowSourceFile = sourceFileActiveSheet.max_row
        maxColSourceFile = sourceFileActiveSheet.max_column
        for rowNum in range(1, maxRowSourceFile + 1):
            for colNum in range(10, maxColSourceFile + 1): # copy everything after column 9
                value = sourceFileActiveSheet.cell(row=rowNum, column=colNum).value
                targetFileActiveSheet.cell(row=rowNum, column=colNum + 3, value=value) # move everything right 3 columns

    print('Saving file as ' + outputFile)
    targetFile.save(pathToExcel + outputFile)
    
    print('Step 1 done, old data copied')
Reply


Messages In This Thread
RE: How to Copy Single Value From One Excel Sheet to Another - by SunWers - Dec-29-2020, 05:39 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 282 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 446 Feb-07-2024, 12:24 PM
Last Post: Viento
  How to copy work sheet data one workbook to other? sayyedkamran 2 706 Nov-03-2023, 09:10 AM
Last Post: Larz60+
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,881 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 2,066 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,040 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  is it possible to copy image from email and place into excel file? cubangt 3 1,280 Nov-30-2022, 05:11 PM
Last Post: snippsat
  Reading Excel file and use a wildcard in file name and sheet name randolphoralph 6 7,131 Jan-13-2022, 10:20 PM
Last Post: randolphoralph
  Python script for excel sheet Nabil 4 3,291 Jun-01-2021, 05:09 AM
Last Post: Pedroski55
  Copy column from one existing excel file to another file mkujawsk 0 5,649 Apr-14-2021, 06:33 PM
Last Post: mkujawsk

Forum Jump:

User Panel Messages

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