1
votes

I am using trying to export using openpyxl and the new pivot table functionality.

I currently have a workbook with two sheets (RawData and Pivot) I load in the workbook, put data into raw data, close and export.

The Pivot table automatically updates the data when i open the file, however it shows the PivotTable fields / editing options on the right side bar. Does anyone know how to remove this?

Preferably I would like a solution using openpyxl

wb = load_workbook(filename=template_file_path)

wb.remove_sheet(wb["RawData"])
ws = wb.create_sheet("RawData")
ws = wb["RawData"]

# ws.title = "RawData"
# #Add Headers
ws.append(data['headers'][1:])
# #Add raw data
for row in data['data']:
    ws.append(row[1:])




#File settings
file_name = str(template_file_path).replace(' ', '_') + "_export.xlsx"
response = HttpResponse(save_virtual_workbook(wb), content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename= "{0}"'.format(file_name)
return response
2

2 Answers

1
votes

After the first line you can add

ws_pivot = wb["Pivot"]
pivot = ws_pivot._pivots[0] 
pivot.disableFieldList = True

to achieve this.

0
votes

You can remove the pivot tables from a worksheet by setting the collection to an empty list: ws._pivots = []