Python Forum
Find last filled column in openpyxl - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Find last filled column in openpyxl (/thread-36076.html)



Find last filled column in openpyxl - Irv1n - Jan-14-2022

I work with excel file and know filename and sheet name of this file. How i can search last filled column and insert my data in next column of this file? I work with openpyxl library.


RE: Find last filled column in openpyxl - BashBedlam - Jan-14-2022

This will illustrate the basic concept.
from openpyxl import load_workbook

reference_letters = [chr (x) for x in range (65, 91)]
workbook_name = 'sample.xlsx'
workbook = load_workbook (filename = workbook_name)
sheet = workbook.active

for letter in reference_letters :
	cell = letter + '1'
	if sheet [cell].value == None :
		sheet [cell] = 'New Data'
		break

workbook.save (filename = 'new_' + workbook_name)



RE: Find last filled column in openpyxl - Pedroski55 - Jan-16-2022

These 2 give you the maximum column and row:

maxRowSourceFile = sourceFile[sheet].max_row
maxColSourceFile = sourceFile[sheet].max_column
This will copy the last column and put it in the column after that.

def myApp():
    import openpyxl

    # the folder with the XL file
    folder = 'temp'
    # the course
    course = '20BE'
    pathToTT = f'/home/pedro/{folder}/{course}/'
    tt = f'{folder}_{course}latestPyMade_newest.xlsx'
    sourceFile = openpyxl.load_workbook(pathToTT + tt)
    sourceSheetNames = sourceFile.sheetnames
    for sheet in sourceSheetNames:       
        maxRowSourceFile = sourceFile[sheet].max_row
        maxColSourceFile = sourceFile[sheet].max_column
        print('Maximum row for sheet', sheet, 'is', maxRowSourceFile)
        print('Maximum column for sheet', sheet, 'is', maxColSourceFile)
        
    for sheet in sourceSheetNames:       
        maxRowSourceFile = sourceFile[sheet].max_row
        maxColSourceFile = sourceFile[sheet].max_column
        for rowNum in range(1, maxRowSourceFile + 1): # +1 to get the last row
            # get the value in the last column
            last_col_value = sourceFile[sheet].cell(row=rowNum, column=maxColSourceFile).value
            # write the value of the last column to the next column
            # the values will be written without any kind of formatting
            sourceFile[sheet].cell(row=rowNum, column=maxColSourceFile+1, value=last_col_value)

    # openpyxl does not have a file.close() open files remain open until your program is finished
    savename = 'changed_sourceFile.xlsx'
    sourceFile.save(pathToTT + savename)
    print('Last column copied to the next column up and saved to', pathToTT + savename)
    print('All done!')
For formatting from openpyxl look at:

from openpyxl.styles import PatternFill
from openpyxl.styles import Font
from openpyxl.utils import get_column_letter
from openpyxl.styles import Alignment  
from openpyxl.drawing.image import Image