2017-07-01 7 views
0

は、私は1つのExcelファイルを持っているopenpyxlチャートの日付形式を表示するにははどのよう

グラフ

私のコード:

from openpyxl import load_workbook 
from openpyxl import Workbook 
from openpyxl.chart import (
ScatterChart, 
Reference, 
Series, 
) 

wb = load_workbook(filename = 'cat-test.xlsx') 
ws = wb.get_sheet_by_name('ACZ') 
chart = ScatterChart() 
chart.title = "Scatter Chart" 
chart.style = 13 
chart.x_axis.title = 'Date' 
chart.y_axis.title = 'Count' 
chart.x_axis.number_format ='yyyy/mm/dd' 
xvalues = Reference(ws, min_col=2, min_row=2, max_row=9) 
for i in range(1, 2): 
    values = Reference(ws, min_col=i, min_row=1, max_row=9) 
    series = Series(values, xvalues, title_from_data=True) 
    chart.series.append(series) 
ws.add_chart(chart, "A16") 
wb.save("cat-test.xlsx") 

私はそれを解決するために助けてください、年は1900年である理由を、ありがとうございました!

答えて

0

私はそれを解決すると思います。私の参照は次のとおりです。 http://openpyxl.readthedocs.io/en/default/charts/line.html#id1

私の新しいコード:

from openpyxl.chart.axis import DateAxis 
from openpyxl import load_workbook 
from openpyxl import Workbook 
from openpyxl.chart import (
    LineChart, 
    Reference, 
    Series, 
) 
wb = load_workbook(filename = 'cat-test.xlsx') 
ws = wb.get_sheet_by_name('ACZ') 
chart = LineChart() 
chart.title = "Line Chart" 
chart.style = 13 
chart.x_axis.title = 'Date' 
chart.y_axis.title = 'Count' 
chart.y_axis.crossAx = 500 
chart.x_axis = DateAxis(crossAx=100) 
chart.x_axis.number_format ='yyyy/mm/dd' 
chart.x_axis.majorTimeUnit = "days" 
data = Reference(ws, min_col=1, min_row=1, max_row=9) 
chart.add_data(data, titles_from_data=True) 
dates = Reference(ws, min_col=2, min_row=2, max_row=9) 
chart.set_categories(dates) 
ws.add_chart(chart, "A16") 
wb.save("cat-test.xlsx") 

my excel line chart