2016-09-26 78 views
1

好吧,所以我對整體excel比較陌生,並被要求編寫一個腳本從數據庫中取出一些數據並製作一張表格。我做了什麼,現在他們要求我添加圖表,以更簡單的格式顯示這些數據,因爲您知道範圍蠕變等等。無論如何,我正在嘗試創建一個餅圖,它引用兩個不是彼此相鄰的單獨表單中的兩個特定單元格。我可以弄清楚這兩個單元之間的一切範圍,但我需要它專門只使用我提供的單元。使用xlsxwriter從指定單元格創建圖表

executive = book.add_worksheet("Executive Summary") 
yesterday_chart = book.add_chart({'type': 'pie'}) 
yesterday_chart.add_series({'name': 'Yesterdays Installs', 
          'values': '=(\'Total Counts\'!$B$2,\'Total Counts\'!$J$2)', 
          'points': [ 
           {'fill': {'color': 'green'}}, 
           {'fill': {'color': 'red'}}, 
          ], 
          }) 

這是我現在有創造兩個點,但沒有填充數據的地方。我需要它從名爲Total Counts的表中繪製出單元格B2和J2。預先感謝您的幫助,以保持我的理智。

編輯:添加更多的代碼並顯示輸出。

#!/usr/bin/python3 

import xlsxwriter 

book = xlsxwriter.Workbook("Testing.xlsx") 

counts = book.add_worksheet("Total Counts") 
r = 0 
c = 0 
counts.write(0, 0, "Total Installed to date") 
counts.write(0, 1, "Installed Yesterday") 
counts.write(0, 2, " Missed Yesterday") 
counts.write(0, 3, "Scheduled Yesterday") 

executive = book.add_worksheet("Executive Summary") 
yesterday_chart = book.add_chart({'type': 'pie'}) 

r += 1 
counts.write(r, c, "100") 
counts.set_column(c, c, len("Total Installed to date")) 
c += 1 
counts.write(r, c, "25") 
counts.set_column(c, c, len("Installed Yesterday")) 
c += 1 
counts.write(r, c, "5") 
counts.set_column(c, c, len("Missed Yesterday")) 
c += 1 
counts.write(r, c, "30") 
counts.set_column(c, c, len("Scheduled Yesterday")) 
c = 0 

yesterday_chart.add_series({'name': 'Yesterdays Installs', 
          'values': "=('Total Counts'!$D$2,'Total Counts'!$B$2)", 
          'points': [ 
           {'fill': {'color': 'green'}}, 
           {'fill': {'color': 'red'}}, 
          ], 
          }) 

executive.insert_chart(0, 0, yesterday_chart) 



book.close() 

Total sheet

Output from graph

回答

2

這句法應該工作。也許發佈一個更完整的工作示例,以便我們可以看到問題所在。

在此期間,在這裏是一個工作示例:

import xlsxwriter 

workbook = xlsxwriter.Workbook('chart.xlsx') 
worksheet = workbook.add_worksheet('Total Counts') 

# Create a new Chart object. 
chart = workbook.add_chart({'type': 'pie'}) 

# Write some data to add to plot on the chart. 
worksheet.write_column('A1', [2, 4, 6, 8, 10]) 

# Add a data series bases on some points. 
chart.add_series({'values': "=('Total Counts'!$A$1,'Total Counts'!$A$3)"}) 

# Insert the chart into the worksheet. 
worksheet.insert_chart('C3', chart) 

workbook.close() 

輸出: enter image description here

+0

我已經添加更多的代碼的請求和連接的圖像。我真的不確定我做錯了什麼。感謝您查看它。 – leigon

+0

您發佈的附加代碼無助於澄清問題。您需要像我一樣發佈[最小化,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – jmcnamara

+0

但是,如果我不得不猜測,我會說在單元格B2和J2中沒有任何數據。 – jmcnamara

相關問題