I need a table inside an Excel file, like this. I can do it partially with this code using openpyxl:
import openpyxl
from openpyxl import Workbook
from openpyxl.worksheet.table import Table, TableStyleInfo
wb = Workbook()
ws = wb.active
data = [
['G0', 'ASP', 'FRA', 'NAC', 'NAC', 'ASP','WHT2.5', 2.5],
['G17179136', 'ASP', 'FRA', 'NAC', 'NAC', 'ASP','WHT2.5', 2.5],
['G17220207', 'ASP', 'FRA', 'NAC', 'NAC', 'ASP','WHT2.5', 2.5],
['G17693868', 'ASP', 'FRA', 'NAC', 'NAC', 'ASP','WHT2.5', 2.5],
]
ws.append(["Nº", "Grupo contable proveedor", "Cód. idioma",
"Grupo contable negocio", "Grupo registro IVA neg.",
"Tipo prov. fact.", "Cód IRPF", "IRPF %"])
for row in data:
ws.append(row)
tab = Table(displayName="Table5", ref="A1:H4")
style = TableStyleInfo(name='TableStyleMedium2', showFirstColumn=False,
showLastColumn=False, showRowStripes=True, showColumnStripes=False)
tab.tableStyleInfo = style
ws.add_table(tab)
wb.save("tablaprueba.xlsx")
So i get a table with headers in the first row, but I need two rows above it, so I thought I could add them later, like this:
wb = openpyxl.load_workbook("tablaprueba.xlsx", data_only=True)
ws = wb.active
ws.insert_rows(1,2)
ws["A1"] = "PROVEEDORES"
ws["B1"] = "Proveedor"
ws["C1"] = 23
wb.save("tablaprueba2.xlsx")
Sadly, that makes the file corrupted. How can I make a table inside excel, with rows above the table headers?