【发布时间】:2019-01-27 22:55:57
【问题描述】:
2017 年 9 月的另一个问题解决了同样的问题,但没有答案:create a pivotchart with python win32com
我已经尝试了几种方法来使其工作,所以我想解释这些,并希望从某人那里获得一些关于如何让这个工作的见解。这似乎不是一条陈旧的道路,所以我不抱太大希望。
环境详情:
- Windows 10
- Office 2013
- Anaconda 3.6
我用
win32com.client.gencache.EnsureDispatch('Excel.Application')
但我也可以使用
win32com.client.DispatchEx('Excel.Application')
或
win32com.client.dynamic.Dispatch('Excel.Application')
每一个都返回这个CLSID win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x8
每一个也有同样的错误。
我还按照文档 here 运行了这个命令:
C:\Users\home_dir>python AppData\Local\Continuum\anaconda3\pkgs\pywin32-223-py36hfa6e2cd_1\Lib\site-packages\win32com\client\makepy.py -i "Microsoft Excel 15.0 Object Library"
Output generated from makepy.py:
Microsoft Excel 15.0 Object Library {00020813-0000-0000-C000-000000000046}, lcid=0, major=1, minor=8
>>> # Use these commands in Python code to auto generate .py support
>>> from win32com.client import gencache
>>> gencache.EnsureModule('{00020813-0000-0000-C000-000000000046}', 0, 1, 8)
这个版本的gencache没有成功:
Excel = win32com.client.gencache.EnsureModule('{00020813-0000-0000-C000-000000000046}', 0, 1, 8)
源数据 我使用 ExcelWriter 写入 Excel 的 Pandas 数据框中有 100 行和 18 列
ew = pd.ExcelWriter('c:\devworkspace\SQL-Pandas-Excel\SampleData.xlsx')
sample_data_df.to_excel(ew, sheet_name='Source Data')
ew.save()
ew.close()
Excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
win32c = win32com.client.constants
wb = Excel.Workbooks.Open('c:\devworkspace\SQL-Pandas-Excel\SampleData.xlsx')
src_sheet = wb.Worksheets('Source Data')
rng_row = 100
rng_col = 18
rng_beg = src_sheet.Cells(1,2)
rng_end = src_sheet.Cells(rng_row,rng_col)
pvt_src_rng = src_sheet.Range(rng_beg, rng_end)
pvt_src_rng.Select()
pvt_src = "%s!R1C2:R%dC%d"%(src_sheet.Name,rng_row+1,rng_col+1) #add 1 for header and df index
透视缓存 我使用 PivotCaches().Create() 而不是 .Add() 所以我可以指定 Version=win32c.xlPivotTableVersion15 这是 Office 2013 的正确版本。否则它似乎默认为版本 11。
pc = wb.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=pvt_src, Version=win32c.xlPivotTableVersion15)
此更改移动了刻度盘,但没有解决我的问题 - 我仍然收到错误并且图表未创建:
- 当我应用它时,数据透视表上的格式得到了增强 改变。
- 根错误代码从 -2147024809 更改为“The 参数不正确”到root错误码-2146827284
它无法转化为人类可读的信息:
print(win32api.FormatMessage(error.excepinfo[5]))
error: (317, 'FormatMessageW', 'The system cannot find message text for message number 0x%1 in the message file for %2.')
搜索此错误代码 2146827284,讨论似乎与 excel 对象正忙有关。但是 Excel.Visible 设置为 0 - 也是默认值 - 所以它在无头模式下运行。
添加工作表、创建数据透视表、添加字段成功 这些都包含在实际代码中的 try-except 中 - 为简洁起见将其删除。
pvt_sheet = wb.Sheets.Add(After=src_sheet)
pvt_sheet.Name = 'Pivot Sheet'
pvt_rng_beg = pvt_sheet.Cells(2,2)
pvt_rng_end = pvt_sheet.Cells(2,2)
pvt_dest_rng = pvt_sheet.Range(pvt_rng_beg, pvt_rng_end)
pt = pc.CreatePivotTable(TableDestination=pvt_dest_rng,TableName='PivotTable1')
pt.AddFields(RowFields="claimant_type_desc" , ColumnFields="claim_cause_desc" )
pt.AddDataField(Field=pt.PivotFields("total_direct_payment"), Caption="Total Incurred")
我可以将工作表或图表添加为“ChartDestination”,但是这两个选项都不会改变结果。我可以验证对象是否已成功添加。
#chrt_sheet = wb.Charts.Add(After=pvt_sheet)
chrt_sheet = wb.Sheets.Add(After=pvt_sheet)
chrt_sheet.Name = 'Pivot Chart'
ChartDestination 参数是唯一必需的参数,其他参数是可选的:XlChartType、Left、Top、Width、Height
文档here:
基于示例,我将工作表或图表对象的名称作为字符串“数据透视表”传递。应该可以的。
pch = pc.CreatePivotChart(ChartDestination='Pivot Chart')
因为参数被定义为 Variant,所以我将字符串显式分配给 Variant 对象。我尝试了一系列不同的变体类型,但没有产生不同的结果。
chrt_sheet_name_variant = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_BSTR, chrt_sheet.Name)
print(chrt_sheet_name_variant.value)
print(chrt_sheet_name_variant.varianttype)
print(chrt_sheet_name_variant.__class__)
print(type(chrt_sheet_name_variant))
pch = pc.CreatePivotChart(ChartDestination=chrt_sheet_name_variant)
#Output:
#Pivot Chart
#16392
#<class 'win32com.client.VARIANT'>
#<class 'win32com.client.VARIANT'>
这是生成的错误:
File "C:\Users\xxxxxx\AppData\Local\Temp\gen_py\3.6\00020813-0000-0000-C000-000000000046x0x1x8\PivotCache.py", line 36, in CreatePivotChart
, XlChartType, Left, Top, Width, Height
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146827284), None)
【问题讨论】:
标签: excel python-3.x pivot-table win32com pivot-chart