title: How do you replace data for an existing line chart using python-pptx?
body: <p>I have a prebuilt and populated powerpoint presentation where I am modifying the data for charts and tables. I would like to retain all formatting (and much of the text), but replace the data in a line chart within a slide.</p>
  
  <p>I have a function that will replace the data using a pandas data frame that works with bar charts.</p>
  
  <pre><code>def replaceCategoryChart(df, chart, skipLastCol=0):
      """
      Replaces Category chartdata for a simple series chart. e.g. Nonfarm Employment
  
      Parameters:
      df: dataframe containing new data. column 0 is the categories
      chart: the powerpoint shape chart.
      skipLast: 0=don't skip last column, 1+ will skip that many columns from the end
  
      Returns: replaced chart(?)
      """
      cols1= list(df)
      #print(cols1)
      #create chart data object
      chart_data = CategoryChartData()
      #create categories
      chart_data.categories=df[cols1[0]]
      # Loop over all series
      for col in cols1[1:-skipLastCol]:
          chart_data.add_series(col, df[col])
      #replace chart data
      chart.replace_data(chart_data)
  </code></pre>
  
  <pre><code>...
  S0_L=  pd.read_excel(EXCEL_BOOK, sheet_name="S0_L", usecols="A:F")
  S0_L_chart = prs.slides[0].shapes[3].chart
  print(S0_L)
  replaceCategoryChart(S0_L, S0_L_chart)
  ...
  </code></pre>
  
  <p>The python file runs successfully, however, when I open the powerpoint file I get the error</p>
  
  <pre><code>Powerpoint found a problem with content in Name.pptx. 
  
  Powerpoint can attempt to repair the presentation.
  
  If you trust the source of this presentation, click Repair.
  </code></pre>
  
  <p>After clicking repair, the slide I attempted to modify is replaced by a blank layout.</p>
  
  <p>Because this function works for bar charts, <strong>I think there is a mistake in the way I am understanding how to use replace_data() for a line chart.</strong></p>
  
  <p>Thank you for your help!</p>