You can achieve this by using insert_rows()
and delete_rows()
...
Note that you need to save the file once you have added/deleted the rows.
from openpyxl import load_workbook
data_file='test.xlsx'
# Load the entire workbook.
wb = load_workbook(data_file)
skip = True
for ws in wb.worksheets:
if skip == True:
skip = False
else:
CurrentRow = 0
for row in ws.iter_rows(max_col=2):
if row[0].value == 'Field Name' and row[1].value == 'Type':
CurrentRow = row[0].row
break
else:
pass
if CurrentRow > 7:
ws.delete_rows(7, CurrentRow - 7)
elif CurrentRow < 7 and CurrentRow > 0:
ws.insert_rows(CurrentRow, 7 - CurrentRow)
wb.save('test.xlsx')
Dealing with tables
It looks like the input data in your sheet is a excel Table. You can check this by selecting the range and right-clicking (should have table option under Quick Analysis). If this is the case, you have two options.
- Select a cell in table >> right click >> Table >> Convert to Range. Then the original code will run. Don't know if that works.
- Written below is the code that will work if all your sheets have tables. Note that I am considering that there is only one table in each sheet. Also, the style is set to the blue format you have shared in your pics above. Borrowed code from here
from openpyxl import load_workbook
from openpyxl.worksheet.table import Table, TableStyleInfo
style = TableStyleInfo(name="TableStyleMedium9", showFirstColumn=False, showLastColumn=False, showRowStripes=True, showColumnStripes=False)
def colnum_string(n):
string = ""
while n > 0:
n, remainder = divmod(n - 1, 26)
string = chr(65 + remainder) + string
return string
#data_file='test.xlsx'
data_file = input("Please provide the name of file you want to process: ")
# Load the entire workbook.
wb = load_workbook(data_file)
skip = True
for ws in wb.worksheets:
if skip == True:
skip = False
else:
CurrentRow = 0
tablelen = 0
for row in ws.iter_rows(max_col=2):
if row[0].value == 'Field Name' and row[1].value == 'Type':
CurrentRow = row[0].row
tablelen = ws.max_row - CurrentRow
break
else:
pass
if CurrentRow > 7:
ws.delete_rows(7, CurrentRow - 7)
resTable = Table(displayName=ws.tables.items()[0][0], ref="A7:{}{}".format("B", 7+tablelen))
resTable.tableStyleInfo = style
ws._tables[ws.tables.items()[0][0]] = resTable
elif CurrentRow < 7 and CurrentRow > 0:
ws.insert_rows(CurrentRow, 7 - CurrentRow)
resTable = Table(displayName=ws.tables.items()[0][0], ref="A7:{}{}".format("B", 7+tablelen))
resTable.tableStyleInfo = style
ws._tables[ws.tables.items()[0][0]] = resTable
#wb.save('test.xlsx')
wb.save(data_file.split('.')[0] + "_updated." + data_file.split('.')[1])
New Req - Read all xlsx files
from openpyxl import load_workbook
from openpyxl.worksheet.table import Table, TableStyleInfo
style = TableStyleInfo(name="TableStyleMedium9", showFirstColumn=False, showLastColumn=False, showRowStripes=True, showColumnStripes=False)
def colnum_string(n):
string = ""
while n > 0:
n, remainder = divmod(n - 1, 26)
string = chr(65 + remainder) + string
return string
import os
ALLOWED_EXTENSIONS = set(['xlsx'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
MyPWD = os.getcwd()
for filename in os.listdir(MyPWD):
path = os.path.join(MyPWD, filename)
if os.path.isfile(path) and allowed_file(filename):
#data_file='test1.xlsx'
#data_file = input("Please provide the name of file you want to process: ")
# Load the entire workbook.
wb = load_workbook(filename)
skip = True
for ws in wb.worksheets:
if skip == True:
skip = False
else:
CurrentRow = 0
tablelen = 0
for row in ws.iter_rows(max_col=2):
if row[0].value == 'Field Name' and row[1].value == 'Type':
CurrentRow = row[0].row
tablelen = ws.max_row - CurrentRow
break
else:
pass
if CurrentRow > 7:
ws.delete_rows(7, CurrentRow - 7)
resTable = Table(displayName=ws.tables.items()[0][0], ref="A7:{}{}".format("B", 7+tablelen))
resTable.tableStyleInfo = style
ws._tables[ws.tables.items()[0][0]] = resTable
elif CurrentRow < 7 and CurrentRow > 0:
ws.insert_rows(CurrentRow, 7 - CurrentRow)
resTable = Table(displayName=ws.tables.items()[0][0], ref="A7:{}{}".format("B", 7+tablelen))
resTable.tableStyleInfo = style
ws._tables[ws.tables.items()[0][0]] = resTable
#wb.save('test2.xlsx')
wb.save(filename.split('.')[0] + "_updated." + filename.split('.')[1])