I solved the problem by adding the column then increasing the table size and then saving the hole thing. To do it again i update the worksheet and repeat the procedure. Of cause you can just add multiple colums at one and increase the table size by more then one column.
Here are the three functions i wrote for this. The addShit() function is copied from a link! So you only need an empty xlsx file to try this example.
Imports:
import os
from openpyxl import load_workbook
from openpyxl.worksheet.table import Table, TableStyleInfo
def addShit(ws):
ws.append(["Fruit", "2011", "2012", "2013", "2014","2011", "2012", "2013", "2014"])
data = [
['Apples', 10000, 5000, 8000, 6000],
['Pears', 2000, 3000, 4000, 5000],
['Bananas', 6000, 6000, 6500, 6000],
['Oranges', 500, 300, 200, 700],
['Apples', 10000, 5000, 8000, 6000],
['Pears', 2000, 3000, 4000, 5000],
['Bananas', 6000, 6000, 6500, 6000],
['Oranges', 500, 300, 200, 700],
]
for row in data:
ws.append(row)
tab = Table(displayName="Table1", ref="A1:E5")
style = TableStyleInfo(name="TableStyleMedium9", showFirstColumn=False,
showLastColumn=False, showRowStripes=True, showColumnStripes=True)
tab.tableStyleInfo = style
ws.add_table(tab)
def update(xlsxPfad, sheetName):
wb = load_workbook(filename=xlsxPfad)
ws = wb[sheetName]
wb.save(xlsxPfad)
return wb,ws
def replaceTab(ws, ref, displayName):
#copys autofilter
autofilter = ws.tables[displayName].autoFilter
#copy current style
style = ws.tables[displayName].tableStyleInfo
#create new enlarged table
entab = Table(displayName=displayName, ref=ref)
entab.tableStyleInfo = style
#change current table to new enlarged table
ws.tables[displayName] = entab
def addCol(ws, colNr ,headerRow , headerVal):
ws.insert_cols(colNr)
ws.cell(row=headerRow, column=colNr).value = headerVal
if __name__ == "__main__":
UserName = os.getlogin()
#empty xlsx file
xlsxPfad = "C:\\Users\\" + str(UserName) + "\\Downloads\\book1.xlsx"
wb = load_workbook(filename=xlsxPfad)
ws = wb["Sheet1"]
#Add data to empty xlsx sheet1
addShit(ws)
wb.save(xlsxPfad)
#update workbook and worksheet
wb, ws = update(xlsxPfad, "Sheet1")
#add column in column 2
addCol(ws, 2, 1, "NewCol1")
# update table size
replaceTab(ws, "A1:F5", "Table1")
wb.save(xlsxPfad)
#update workbook and worksheet
wb, ws = update(xlsxPfad, "Sheet1")
# add column in column 2
addCol(ws, 2, 1, "NewCol2")
#update table size
replaceTab(ws, "A1:G5", "Table1")
wb.save(xlsxPfad)
os.startfile(xlsxPfad)