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