通过Python将chartobject添加到excel

时间:2015-07-10 19:09:55

标签: python excel

所以我一直在尝试使用IronPython将图表对象添加到Excel文件中,每当我调用ws.ChartObjects时我都会收到错误。由于某种原因,它告诉我它是一个DispCallable并且它没有Add属性。

clr.AddReferenceByName('Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c')
from Microsoft.Office.Interop import Excel

System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo("en-US")
from System.Runtime.InteropServices import Marshal


def SetUp(xlApp):
    # supress updates and warning pop ups
    xlApp.Visible = False
    xlApp.DisplayAlerts = False
    xlApp.ScreenUpdating = False
    return xlApp

def ExitExcel(filePath, xlApp, wb, ws):
    # clean up before exiting excel, if any COM object remains
    # unreleased then excel crashes on open following time
    def CleanUp(_list):
        if isinstance(_list, list):
            for i in _list:
                Marshal.ReleaseComObject(i)
        else:
            Marshal.ReleaseComObject(_list)
        return None

    wb.SaveAs(str(filePath))
    xlApp.ActiveWorkbook.Close(False)
    xlApp.ScreenUpdating = True
    CleanUp([ws,wb,xlApp])
    return None

def GetWidthHeight(origin, extent, ws):
    left = ws.Cells(bb.xlRange(cellRange)[1], bb.xlRange(cellRange)[0]).Left
    top = ws.Cells(bb.xlRange(cellRange)[1], bb.xlRange(cellRange)[0]).Top
    width = ws.Range[origin, extent].Width
    height = ws.Range[origin, extent].Height
    return [left, top, width, height]

if runMe:
    message = None
    try:
        xlApp = SetUp(Excel.ApplicationClass())
        errorReport = None
        xlApp.Workbooks.open(str(filePath))
        wb = xlApp.ActiveWorkbook
        ws = xlApp.Sheets(sheetName)
        # i have no clue why ws.ChartObjects.Count throws an error all the time
        origin = ws.Cells(bb.xlRange(cellRange)[1], bb.xlRange(cellRange)[0])
        extent = ws.Cells(bb.xlRange(cellRange)[3], bb.xlRange(cellRange)[2])
        left = GetWidthHeight(origin, extent, ws)[0]
        top = GetWidthHeight(origin, extent, ws)[1]
        width = GetWidthHeight(origin, extent, ws)[2]
        height = GetWidthHeight(origin, extent, ws)[3]
        xlChartObject = ws.ChartObjects.Add(int(left), int(top), int(width), int(height))
        Marshal.ReleaseComObject(extent)
        Marshal.ReleaseComObject(origin)
        ExitExcel(filePath, xlApp, wb, ws)
    except:
        # if error accurs anywhere in the process catch it
        import traceback
        errorReport = traceback.format_exc()

我的问题是调用ws.ChartObjects.Add()会抛出异常'DispCallable'对象没有属性'Add'。我该如何解决这个问题?怎么了?

2 个答案:

答案 0 :(得分:1)

根据similar issue表示ChartObjects是一个功能,你应该使用

ChartObjects().Add(...)

答案 1 :(得分:0)

根据official documentation,参数应为double。如果这不是问题,您可以拆分

xlChartObject = ws.ChartObjects.Add(...

xlChartObjects = ws.ChartObjects
xlChartObject = xlChartObjects.Add(...

开始调试。这是一个好主意:

  1. 使用How do I get list of methods in a Python class?Finding what methods an object has检查可用的方法(适用于xlChartObjects类)。
  2. 使用What's the canonical way to check for type in python?检查类型。
  3. 您可能会学习如何解决这些问题。

    PS:您发布的代码sheetNamebb未定义,尽管您可能是先前定义的。