Home » excel » python – 'xlCategory' and 'xlValue' is not defined in xlwings Charts API implementation

python – 'xlCategory' and 'xlValue' is not defined in xlwings Charts API implementation

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to do a Charts API implementation in xlwings – python, for basic chart manipulation like adding axis title, changing the line colors, plot marker size etc.

I get an error:

name ‘xlCategory’ is not defined

The code for implementation is

import xlwings as xw

wb = xw.Book(r'Tau.xlsm') 
sht = wb.sheets.add(name ='Plot')
tau_plot = sht.charts.add()
tau_plot.chart_type='xy_scatter'
tau_plot.set_source_data(sht.range('E1:F135'))
tau_plot.api[1].Axes(xlCategory).HasTitle = True

Can you please help me with this error.

How to&Answers:

xlCategory is defined under XlAxisType, an enum that defines a number of constant values. If you’re not referencing the Excel type library / object model, xlCategory means nothing to Python/xlwings. Use its underlying value instead (1), or define your own copy so that the identifier xlCategory is associated with the value 1.

Answer:

You can use Excel’s constants as follows:

>>> from xlwings.constants import AxisType
>>> AxisType.xlCategory