使用Python 3.5和xlsxwriter 0.9.6。
我在工作表中插入文本框时遇到问题,当同一工作簿中还有一个图表表时。
下面我使用了xlsxwriter文档(http://xlsxwriter.readthedocs.io/example_chart_data_table.html)中的示例,并在文本框中添加到工作表部分。
# Copyright 2013-2016, John McNamara, jmcnamara@cpan.org
#
import xlsxwriter
workbook = xlsxwriter.Workbook('chartsheet.xlsx')
# Add a worksheet to hold the data.
worksheet = workbook.add_worksheet()
# Add a chartsheet. A worksheet that only holds a chart.
chartsheet = workbook.add_chartsheet()
# Add a format for the headings.
bold = workbook.add_format({'bold': 1})
# Add the worksheet data that the charts will refer to.
headings = ['Number', 'Batch 1', 'Batch 2']
data = [
[2, 3, 4, 5, 6, 7],
[10, 40, 50, 20, 10, 50],
[30, 60, 70, 50, 40, 30],
]
worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])
#Section I've added in
tb_options = {
'width': 10,
'height': 10,
'x_offset': 5,
'y_offset': 5,
'fill': {'color': 'gray'}}
worksheet.insert_textbox('A15', " ", tb_options)
#End of Section I've added in
# Create a new bar chart.
chart1 = workbook.add_chart({'type': 'bar'})
# Configure the first series.
chart1.add_series({
'name': '=Sheet1!$B$1',
'categories': '=Sheet1!$A$2:$A$7',
'values': '=Sheet1!$B$2:$B$7',
})
# Configure a second series. Use of alternative syntax define ranges.
chart1.add_series({
'name': ['Sheet1', 0, 2],
'categories': ['Sheet1', 1, 0, 6, 0],
'values': ['Sheet1', 1, 2, 6, 2],
})
# Add a chart title and some axis labels.
chart1.set_title ({'name': 'Results of sample analysis'})
chart1.set_x_axis({'name': 'Test number'})
chart1.set_y_axis({'name': 'Sample length (mm)'})
# Set an Excel chart style.
chart1.set_style(11)
# Add the chart to the chartsheet.
chartsheet.set_chart(chart1)
workbook.close()
excel生成的错误说: "我们发现“chartheet.xlsx”中的某些内容存在问题。您是否希望我们尝试尽可能多地恢复?如果您信任此工作簿的来源,请单击是。"
如果可能,我想避免出现此错误。如果我从代码中省略了图表,只生成了工作表,那么就会毫无问题地添加text_box。
是否有人知道一个解决方案,除了同一工作簿中的图表之外,您还可以在其中包含一个带有文本框的工作表?