0

I am trying to generate a sales graph using xlsxwriter. In y-axis, the values are coming as 20000, 40000 etc. I want it come as 10k, 20k etc.

Is it possible to do same in excel using xlsxwriter.

enter image description here

Kshitish Das
  • 67
  • 1
  • 7

1 Answers1

1

You can use format cell in xlsxwriter package (see following sample code):

rowCounter = 0
formatCell = workbook.add_format({ 'num_format'  : '0.00"k"' })
for _, colValue in (dataToWrite) : 
    worksheet.write(rowCounter, 0, colValue/1000,formatCell)
    rowCounter += 1

chart = workbook.add_chart({'type' : 'line'})
chart.add_series({"values" : "=Sheet1!$A$1:$A$4"})

worksheet.insert_chart('D1' , chart)

NOTE : Above code tested on xlsxwriter version : 1.2.9

Ali Bigdeli
  • 1,286
  • 3
  • 17
  • 35
  • Thanks Ali, Graph is coming fine now, but when the value is coming below 1000, it taking them as 0, is there any way to handle same. Thanks again. – Kshitish Das Jul 12 '20 at 19:12
  • @KshitishDas your wellcome, I can't exactly understand what is your problem. Maybe your data range is too big and less then 1000 is near to x axis, – Ali Bigdeli Jul 13 '20 at 13:25
  • @KshitishDas if you want see more detail in your excel add this ```chart.set_y_axis({'minor_unit': 0.4, 'major_unit': 2, 'minor_tick_mark': 'outside'})``` to your code.For more information see this https://xlsxwriter.readthedocs.io/chart.html#minor_tick_mark – Ali Bigdeli Jul 13 '20 at 13:25