2

I'm gettin this error:

AttributeError: 'Workbook' object has no attribute 'add_chart'

While running this code (python version 3.7.4):

import re
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import numpy

# ... other code here not involved to this part ...

df = pd.DataFrame({"city": city_list, "tourist": tourist_list, "month": month_list})
writer = pd.ExcelWriter('C:\\Users\\portovenere\\Downloads\\exc.xlsx')
workbook = writer.book

chart = workbook.add_chart({'type': 'column'})

chart.add_series({
    'totals':     '=Sheet1!$A$3:$A$21',
    'gap':        2,
})

add_chart usage is explained in this pandas xlsx-writer documentation that I'm following.

I see add_chart is defined in XlsxWriter/workbook.py at line 228

def add_chart(self, options):
        """
        Create a chart object.
        Args:
            options: The chart type and subtype options.
        Returns:
            Reference to a Chart object.
        """

What I'm missing?

Thank you

GabrieleMartini
  • 1,665
  • 2
  • 19
  • 26
  • Well, obviously "workbook" has no attribute "add_chart", did you try to use `dir` on `workbook` to see what's in there? – xpy Jan 22 '20 at 11:09
  • 1
    You probably need to set `xlsxwriter` as the `engine` for ExcelWriter. It is probably using openpyxl instead. Refer to the example you linked to. – jmcnamara Jan 22 '20 at 11:22

1 Answers1

2

Adding this line

import xlsxwriter

as suggested in this Q/A, and specifying xlsxwriter as suggested in the comment, solved the issue.

Thank you again

GabrieleMartini
  • 1,665
  • 2
  • 19
  • 26