Search code examples
pythonexcelvbaopenpyxlpywin32

My 2nd range copys perfect but the first does nothing?


So I've got to different blocks that are nearly identical, the second range copys perfectly but the first does nothing. Any idea whats going on? I've tried everything I can think of. I checked the macros to make sure they weren't clearing the range after it was pasted into the workbook, the macros are stopped before doing anything due to the range being empty.

import win32com.client
import openpyxl

#1st range
wb = openpyxl.load_workbook("C:/Users/tyler/Desktop/Beef Web/Export.xlsx") 
sheet = wb["Export"] 


template = openpyxl.load_workbook(filename='C:/Users/tyler/Desktop/Beef Web/Wall Types Live.xlsm', read_only=False, keep_vba=True) #Add file name
temp_sheet = template["Raw Export"] 



def copyRange(startCol, startRow, endCol, endRow, sheet):
    rangeSelected = []

    for i in range(startRow,endRow + 1,1):

        rowSelected = []
        for j in range(startCol,endCol+1,1):
            rowSelected.append(sheet.cell(row = i, column = j).value)

        rangeSelected.append(rowSelected)

    return rangeSelected




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
def createData():
    print("Processing first...")
    selectedRange = copyRange(1,2,11,100000,sheet) 
    pastingRange = pasteRange(1,12,11,100000,temp_sheet,selectedRange) 

    print("Complete!")

Go = createData()
#Run macros
xl=win32com.client.Dispatch('Excel.Application')
xl.Workbooks.Open(Filename=r'C:\Users\tyler\Desktop\Beef Web\Wall Types Live.xlsm', ReadOnly=0)
xl.Application.Run('Python')
del xl
#2nd range
wb = openpyxl.load_workbook("C:/Users/tyler/Desktop/Beef Web/Wall Types Live.xlsm") 
sheet = wb["E1 Final"] 


template = openpyxl.load_workbook("C:/Users/tyler/Desktop/Beef Web/Export.xlsx") 
temp_sheet = template["Export"] 



def copyRange(startCol, startRow, endCol, endRow, sheet):
    rangeSelected = []

    for i in range(startRow,endRow + 1,1):

        rowSelected = []
        for j in range(startCol,endCol+1,1):
            rowSelected.append(sheet.cell(row = i, column = j).value)

        rangeSelected.append(rowSelected)

    return rangeSelected



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
def createData():
    print("Processing second...")
    selectedRange = copyRange(1,1,19,100000,sheet) 
    pastingRange = pasteRange(1,1,19,100000,temp_sheet,selectedRange) 

    template.save("Finishtest.xlsx")
    print("Complete!")

Go = createData()

Solution

  • @APhillips I finally got it running. openpyxl has an issue not relating to my code so I ended up just doing the whole process with win32com. here is the working code.

    import win32com.client
    import openpyxl
    
    def CopyIn_run_macros():
        #Run macros
        xl=win32com.client.Dispatch('Excel.Application')
        BC=xl.Workbooks.Open('Processing File 1 Path')
        xl.Application.Run('Python2')
        EXP=xl.Workbooks.Open('Import Data File Path')
        print("Importing Export To Processing Sheet....")
        EXP.Sheets('Sheet1').Range('A2:K100000').Copy()
        BC.Sheets('Sheet1').Paste(BC.Sheets('Sheet1').Range('A12'))
        EXP.Close(True)
        print("Complete.")
        print("Running Macros....")
        xl.Application.Run('Python')
        print("Complete.")
        print("Exporting Export Processed Data....")
        FIN=xl.Workbooks.Open('C:/Users/tyler/Desktop/Beef Web/Finish.xlsx')
        BC.Sheets('Sheet2').Range('A1:T100000').Copy()
        FIN.Sheets('Sheet1').Paste(FIN.Sheets('Sheet1').Range('A1'))
        BC.Sheets('Sheet3').Range('A1:H100000').Copy()
        FIN.Sheets('Sheet2').Paste(FIN.Sheets('Sheet2').Range('A1'))
        BC.Sheets('Sheet4').Range('A1:AG100000').Copy()
        FIN.Sheets('Sheet3').Paste(FIN.Sheets('Sheet3').Range('A1'))
        FIN.Close(True)
        print("Complete.")
        del xl
    
    
    CopyIn_run_macros()