【问题标题】:Creating pivot table in Excel using python causes pywintypes.com_error使用 python 在 Excel 中创建数据透视表会导致 pywintypes.com_error
【发布时间】:2020-06-22 15:55:21
【问题描述】:

我修改了here 找到的以下代码,以在我现有的 Excel 工作表中创建数据透视表:

import win32com.client as win32
win32c = win32.constants
import sys
import itertools
tablecount = itertools.count(1)

def addpivot(wb,sourcedata,title,filters=(),columns=(),
         rows=(),sumvalue=(),sortfield=""):

    newsheet = wb.Sheets.Add()
    newsheet.Cells(1,1).Value = title
    newsheet.Cells(1,1).Font.Size = 16
    tname = "PivotTable%d"%tablecount.next()
    pc = wb.PivotCaches().Add(SourceType=win32c.xlDatabase,
                             SourceData=sourcedata)
    pt = pc.CreatePivotTable(TableDestination="%s!R4C1"%newsheet.Name,
                         TableName=tname,
                         DefaultVersion=win32c.xlPivotTableVersion10)
    for fieldlist,fieldc in ((filters,win32c.xlPageField),
                        (columns,win32c.xlColumnField),
                        (rows,win32c.xlRowField)):
        for i,val in enumerate(fieldlist):
            wb.ActiveSheet.PivotTables(tname).PivotFields(val).Orientation = fieldc
            wb.ActiveSheet.PivotTables(tname).PivotFields(val).Position = i+1
    wb.ActiveSheet.PivotTables(tname).AddDataField(wb.ActiveSheet.PivotTables(tname).
                                         PivotFields(sumvalue),sumvalue,win32c.xlSum)


def runexcel():
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    #excel.Visible = True
    try:
        wb = excel.Workbooks.Open('18.03.14.xls')
    except:
        print "Failed to open spreadsheet 18.03.14.xls"
        sys.exit(1)
    ws = wb.Sheets('defaulters')
    xldata = ws.UsedRange.Value
    newdata = []
    for row in xldata:
        if len(row) == 4 and row[-1] is not None:
            newdata.append(list(row))
    rowcnt = len(newdata)
    colcnt = len(newdata[0])
    wsnew = wb.Sheets.Add()
    wsnew.Range(wsnew.Cells(1,1),wsnew.Cells(rowcnt,colcnt)).Value = newdata
    wsnew.Columns.AutoFit()
    src = "%s!R1C1:R%dC%d"%(wsnew.Name,rowcnt,colcnt)
    addpivot(wb,src,
         title="Employees by leads",
         filters=("Leads",),
         columns=(),
         rows=("Name",),
         sumvalue="Actual hours",
         sortfield=())

    if int(float(excel.Version)) >= 12:
        wb.SaveAs('new18.03.14.xlsx',win32c.xlOpenXMLWorkbook)
    else:
        wb.SaveAs('new18.03.14.xls')
    excel.Application.Quit()

if __name__ == "__main__":
    runexcel()

这行代码, wb.ActiveSheet.PivotTables(tname).AddDataField(wb.ActiveSheet.PivotTables(tname).PivotFields(sumvalue),sumvalue,win32c.xlSum)

返回以下错误:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u'PivotFields method of PivotTable class failed', u'xlmain11.chm', 0, -2146827284), None).

当我删除该行时,会生成没有任何数据字段的数据透视表。是不是我做错了什么?

【问题讨论】:

    标签: python excel vba pywin32


    【解决方案1】:

    由于这是从 Python 搜索 Excel 数据透视表时 Google 的第一个热门搜索结果,因此我发布了我的示例代码。此代码通过 COM 服务器在 Excel 中生成一个简单的数据透视表,其中应用了一些基本过滤器、列、行和一些数字格式。 我希望这可以帮助某人不要浪费半天时间(就像我一样......)

    import win32com.client
    Excel   = win32com.client.gencache.EnsureDispatch('Excel.Application') # Excel = win32com.client.Dispatch('Excel.Application')
    
    win32c = win32com.client.constants
    
    wb = Excel.Workbooks.Add()
    Sheet1 = wb.Worksheets("Sheet1")
    
    TestData = [['Country','Name','Gender','Sign','Amount'],
                 ['CH','Max' ,'M','Plus',123.4567],
                 ['CH','Max' ,'M','Minus',-23.4567],
                 ['CH','Max' ,'M','Plus',12.2314],
                 ['CH','Max' ,'M','Minus',-2.2314],
                 ['CH','Sam' ,'M','Plus',453.7685],
                 ['CH','Sam' ,'M','Minus',-53.7685],
                 ['CH','Sara','F','Plus',777.666],
                 ['CH','Sara','F','Minus',-77.666],
                 ['DE','Hans','M','Plus',345.088],
                 ['DE','Hans','M','Minus',-45.088],
                 ['DE','Paul','M','Plus',222.455],
                 ['DE','Paul','M','Minus',-22.455]]
    
    for i, TestDataRow in enumerate(TestData):
        for j, TestDataItem in enumerate(TestDataRow):
            Sheet1.Cells(i+2,j+4).Value = TestDataItem
    
    cl1 = Sheet1.Cells(2,4)
    cl2 = Sheet1.Cells(2+len(TestData)-1,4+len(TestData[0])-1)
    PivotSourceRange = Sheet1.Range(cl1,cl2)
    
    PivotSourceRange.Select()
    
    Sheet2 = wb.Worksheets(2)
    cl3=Sheet2.Cells(4,1)
    PivotTargetRange=  Sheet2.Range(cl3,cl3)
    PivotTableName = 'ReportPivotTable'
    
    PivotCache = wb.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=PivotSourceRange, Version=win32c.xlPivotTableVersion14)
    
    PivotTable = PivotCache.CreatePivotTable(TableDestination=PivotTargetRange, TableName=PivotTableName, DefaultVersion=win32c.xlPivotTableVersion14)
    
    PivotTable.PivotFields('Name').Orientation = win32c.xlRowField
    PivotTable.PivotFields('Name').Position = 1
    PivotTable.PivotFields('Gender').Orientation = win32c.xlPageField
    PivotTable.PivotFields('Gender').Position = 1
    PivotTable.PivotFields('Gender').CurrentPage = 'M'
    PivotTable.PivotFields('Country').Orientation = win32c.xlColumnField
    PivotTable.PivotFields('Country').Position = 1
    PivotTable.PivotFields('Country').Subtotals = [False, False, False, False, False, False, False, False, False, False, False, False]
    PivotTable.PivotFields('Sign').Orientation = win32c.xlColumnField
    PivotTable.PivotFields('Sign').Position = 2
    
    DataField = PivotTable.AddDataField(PivotTable.PivotFields('Amount'))
    DataField.NumberFormat = '#\'##0.00'
    
    Excel.Visible = 1
    
    wb.SaveAs('ranges_and_offsets.xlsx')
    Excel.Application.Quit()
    

    【讨论】:

    • 从这个答案中我学会了如何禁用数据透视表小计。谢谢!
    • 为了让它在 Excel 2013 中工作,在引入 Sheet2 之前添加了wb.Sheets.Add (After=wb.Sheets(1))。
    • 您好先生,我正在尝试您的代码并获得:pywintypes.com_error: (-2147352567, 'Došlo k výjimce.', (0, None, None, None, 0, -2147352565), None) 我承认我几乎是初学者。你有遇到过这种错误信息吗?我有 Excel 2010、win7、python 3.8 32 位。
    • 我正在使用 python 0.16,我在 wb.PivotCaches().Create 和使用枚举值的任何其他行(如 win32c.xlRowField)上遇到错误。用等效的int值替换枚举,解决了问题
    • @TomášKruliš 你找到解决错误的方法了吗,我发现了同样的问题?
    【解决方案2】:

    从PivotTable.AddDataField method (Excel)发现expression .AddDataField(Field, Caption, Function)中只有Field是必需的,其他两个参数是可选的。我删除了它们,代码工作正常!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      相关资源
      最近更新 更多