Python的XlsxWriter插入文本框到图表中

我已经创build了一个列和线图的组合图。 棘手的部分是在图表中插入一个文本框:1.在X轴标题下面2.在图例下面。

我读了文档,我发现worksheet.insert_textbox()。 但是,我需要一些图表(如果我正确理解文档,上面的方法在工作表上,而不是在图表上)。

我在Windows Vista 64位上使用Anaconda Python(2.3.0)。

result = pd.ExcelWriter(r'C:/tmp/dev_excel.xlsx', engine='xlsxwriter') # inserting image with 'xlsxwriter' workbook = result.book worksheet = workbook.add_worksheet() # Add the worksheet data to be plotted. data = ['Aaaa', 'A2', 'A3', 'A4', 20, 10, 50] worksheet.write_column('A1', data) data = ['Bbbbb', 10, 40, 50, 20, 10, 50] worksheet.write_column('B1', data) data = ['Cccc', 110, 140, 150, 120, 110, 150] worksheet.write_column('C1', data) # Create a new column chart. This will use this as the primary chart. column_chart = workbook.add_chart({'type': 'column'}) # Configure the data series for the primary chart. column_chart.add_series({ 'name': '=Sheet1!B1', 'categories': '=Sheet1!A2:A7', 'values': '=Sheet1!B2:B7', }) # Create a new column chart. This will use this as the secondary chart. line_chart = workbook.add_chart({'type': 'line'}) # Configure the data series for the secondary chart. line_chart.add_series({ 'name': '=Sheet1!C1', 'categories': '=Sheet1!A2:A7', 'values': '=Sheet1!C2:C7', 'y2_axis': True, }) # Add a chart title and some axis labels. # ... column_chart.set_y2_axis({'name': 'Target length (mm)'}) # Combine the charts. column_chart.combine(line_chart) # Add a chart title and some axis labels. Note, this is done via the # primary chart. column_chart.set_title({ 'name': 'Combined chart - same Y axis'}) column_chart.set_x_axis({'name': 'Test number'}) column_chart.set_y_axis({'name': 'Sample length (mm)'}) # Insert the chart into the worksheet worksheet.insert_chart('E2', column_chart) workbook.close() result.save() 

如果我正确理解文档,上面的方法工作在工作表上,而不是在图表上

那是对的。 XlsxWriter不支持图表中的文本框。

您可以将工作表文本框覆盖在图表上,但是它们将是两个单独的对象,并将独立于对方移动。