Create a new file called 'demo.py' and copy the following
into it. This should do something close to what it is you want. Hopefully the comments (along with prerequisite googling) will
give you a good indication of what's going on. This will take an existing spreadsheet and the starting row that you what to shift up from. It'll perform the operation and if that starting row # is less than the existing rows, it'll add a blank row and append the rest of the source rows at their original row numbers. To be safe it will dump the results into a new workbook.
import sys
import os
from openpyxl import load_workbook, Workbook
# check that you have 2 command line arguments to use
if len(sys.argv)!=3:
sys.exit("Usage demo.py xls_filename start_line")
# ensure you have an existing file
if not os.path.isfile(sys.argv[1]):
sys.exit("input file does not exist")
excelFile=sys.argv[1]
# make sure the starting row is a number!
if not (sys.argv[2]).isdigit():
sys.exit("2nd argument must be a digit")
num=int(sys.argv[2])
# make sure your extension is okay
root,ext = os.path.splitext(excelFile)
if not ext in ['.xls','.xlsm','.xlsx','.xlsb']:
sys.exit("%s does not have an allowable excel extension"%excelFile)
newExcelFile=root + '_new' + ext
# open the source (1) and destination (2) workbooks & worksheets
wb1 = load_workbook(excelFile)
wb2 = Workbook()
ws1 = wb1.active
ws2 = wb2.active
# move each source row up one in the destination
# starting from row 1 to num
num=min(num,ws1.max_row)
for i in range(1,num):
ws2.append(ws1.rows[i])
if num<ws1.max_row:
# append a blank row
ws2.append(tuple())
# copy the rest of the rows
for i in range(num,ws1.max_row):
ws2.append(ws1.rows[i])
# save the destination workbook
wb2.save(newExcelFile)
Note that you will lose the first row of the source worksheet - which might not be what you want.
I have to add a disclaimer here: I can't vouch for its robustness/completeness as my python is rusty and I've only ever used 'win32com' to do something similar. I'll leave further development (and debugging) to you but let me know if there are issues with it.