NinjaBat
NinjaBat

Reputation: 390

Sort data based on column in xlsxwriter

Is there a way to sort the data before writing into excel using xlsxwriter? I am trying to create a sorted excel report based on a column in odoo.

Upvotes: 1

Views: 1714

Answers (1)

dheinz
dheinz

Reputation: 1096

You could store the data as a pandas dataframe and sort it as indicated on the pandas documentation: http://pandas.pydata.org/pandas-docs/version/0.19/generated/pandas.DataFrame.sort.html

As an example on the website, the exemplary pandas data frame result is sorted as follows (where df is the unsorted data frame):

import pandas as pd
result = df.sort(['A', 'B'], ascending=[1, 0])

Then you can use the pandas Excel Writer and convert the dataframe to an Excel Sheet. Further information is indicated on the pandas documentation: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html

The exemplary pandas data frame result is written into an Excel sheet using the following syntax:

writer = pd.ExcelWriter('output.xlsx')
result.to_excel(writer,'Sheet1')
writer.save()

Upvotes: 2

Related Questions