1

I want to overlay multiple sheets of data on a graph with Xlwings. This is the code I wrote, but of course you get this error.

import xlwings as xw
import pandas as pd

sht = xw.Book().sheets[0]
readdata = pd.read_csv('c:Users/user/test.csv')
xw.Range('A1').value = readdata

chart = sht.charts.add()
chart.set_source_data(xw.sheets[0].range('A3:A50, C3:C50'),xw.sheets[1].range('A3:A50, C3:C50'))

Error Msg.

set_source_data() takes 2 positional arguments but 3 were given

I want to create a graph like this image with xlwings.

enter image description here

I'm doing a project that runs macros in Python. I want to migrate from VBA to Python to simplify macro creation and management.

I'm afraid I can communicate with my ugly English. Please let me know the solution.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • I'm not familiar with this library, but as a general debugging tip you could check `xw.sheets[0].range('A3:A50, C3:C50')` and `xw.sheets[1].range('A3:A50, C3:C50')` to see if they are returning what you expect them to be returning. – liamhawkins Mar 21 '19 at 11:22
  • What is this notation of single quotes around two ranges separated by a comma, i.e. `'A3:A50, C3:C50'`? – Paul Ogilvie Mar 21 '19 at 11:33
  • @PaulOgilvie Since it is Python code you can use [single quotes as well as double quotes](https://stackoverflow.com/a/56190/3219613) and coma seperated addresses in VBA range are handled as you would select both addresses at once. Actually it is a range that consists of 2 [areas](https://learn.microsoft.com/en-us/office/vba/api/excel.range.areas). – Pᴇʜ Mar 21 '19 at 12:14

1 Answers1

0

According to the documentation the

xlwings.Chart.set_source_data(source) 

Sets the source data range for the chart, and accepts one parameter source of type Range eg:

source = xw.books['Book1'].sheets[0].range('A1')

So try instead

chart.set_source_data(xw.sheets[0].range('A3:A50, C3:C50'))
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73