Dynamically produced XLSXWriter charts in python - not referencing

别来无恙 提交于 2019-12-05 14:08:39

Your example invocations:

test.add_chart(df, 'Df Title', 1)
test.add_chart(df2, 'Df2 Title', 1)

suggest that you sometimes had spaces in the names. This results in broken references when you try things like

'name': '=' + tab_name +'!$' + self.letters[n] + '$1',

which would evaluate to

'name': '=Df Title!$A$1',

(when tab_name equals 'Df Title' and n equals 0), for example.

You should still be able to have sheet references with spaces, but enclosed in single-quotes, like

'name': "='Df Title'!$A$1",

so a more robust way to code would be

'name': "='" + tab_name +"'!$" + self.letters[n] + '$1',

I'm a little surprised that the charts would work at all with the broken sheet references, but I haven't actually tested charts myself, just plain cell formulas that involve sheet names.

@John Y is correct that you aren't quoting the worksheet names correctly in chart range references.

You could avoid this issue, and the hand-rolled conversion from numbers to cell references, by using the chart list syntax rather than the string syntax:

chart.add_series({
    'name':       ['Sheet1', 0, col],
    'categories': ['Sheet1', 1, 0,   max_row, 0],
    'values':     ['Sheet1', 1, col, max_row, col],
})

The snippet is from this example in the XlsxWriter docs.

This applies to a few other places in your code as well. As a general rule in XlsxWriter you can use row-column syntax (almost) anywhere you would use A1 syntax: Working with Cell Notation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!