Jun-01-2021, 05:09 AM
I often have to do stuff like this.
Make yourself a function, put it in a module if you do this very often!
Make yourself a function, put it in a module if you do this very often!
import openpyxl # where is my file? path = '/home/pedro/summer2021/19BE/' # which file? myfile = 'test_copy_column.xlsx' # open the file targetFile = openpyxl.load_workbook(path + myfile) # sheet names are always handy targetFileSheetNames = targetFile.sheetnames # get the values in column 1 and write them to column 2 # could also add them or do other tricks for sheet in targetFileSheetNames: # always need the max_row or max_column to do things like this maxRow = targetFile[sheet].max_row for rowNum in range(1, maxRow + 1): content = targetFile[sheet].cell(row=rowNum, column=1).value # this will overwrite without warning if column 2 already has content!! targetFile[sheet].cell(row=rowNum, column=2, value=content) # don't forget to save it targetFile.save(path + myfile)