2017-08-07 37 views
0

的最初來源,以創建一個Excel計算字段對Excel的2個表:Python的 - 如何在不修改數據

tables

我用Python創建一個Excel數據透視表,但我無法找到一個簡單的方式創建裏面計算字段(如我會用VB做)從左邊的表和地區從右表匹配區域。

所以我這樣做,使用模塊win32com.client:

首先,存儲在表的內容在兩個列表:myTable的和myRates。

然後,在原左表中添加了一個新列,其中我計算了CA * (1 + rate)。這裏的代碼:

calField = [['CA Bonifié']] #first element as a title for the new column : 
for a, testMyTable in enumerate(myTable): 
    for b, testMyRates in enumerate(myRates): 
     if a >0 and b > 0: 
      if testMyTable[0] == testMyRates[0]: 
       calField.append([ testMyTable[ len(testMyTable)-1 ] * (1+testMyRates[1]) ]) 

for i, testDataRow in enumerate(calField): 
    for j, testDataItem in enumerate(testDataRow): 
     Sheet1.Cells(i+1,len(testMyTable)+1).Value = testDataItem 

它在片什麼 「源」:

alteration

它在創建表 「TCD」 什麼:

expected result

結果是好的,但我不喜歡這種方法,因爲它改變了原來的表格。所以我正在尋找一個最簡單的方法來做到這一點。

在此先感謝您的幫助

PS:下面的整個代碼。願它有所幫助。

import win32com.client 
Excel = win32com.client.gencache.EnsureDispatch('Excel.Application') 

win32c = win32com.client.constants 

Excel.Visible = True 

wb = Excel.Workbooks.Open('C:/Users/Documents/Python/classeur.xlsx') 

Sheet1 = wb.Worksheets('Source') 

def getContiguousRange(fichier, sheet, row, col): 

    bottom = row 
    while sheet.Cells(bottom + 1, col).Value not in [None, '']: 
     bottom = bottom + 1 

    right = col 
    while sheet.Cells(row, right + 1).Value not in [None, '']: 
     right = right + 1 

    return sheet.Range(sheet.Cells(row, col), sheet.Cells(bottom, right)).Value 

myTable = getContiguousRange(fichier = wb, sheet = Sheet1, row = 1, col = 1) 
myRates = getContiguousRange(fichier = wb, sheet = Sheet1, row = 1, col = 8) 

calField = [['CA Bonifié']] 
for a, testMyTable in enumerate(myTable): 
    for b, testMyRates in enumerate(myRates): 
     if a >0 and b > 0: 
      if testMyTable[0] == testMyRates[0]: 
       calField.append([ testMyTable[ len(testMyTable)-1 ] * (1+testMyRates[1]) ]) 

for i, testDataRow in enumerate(calField): 
    for j, testDataItem in enumerate(testDataRow): 
     Sheet1.Cells(i+1,len(testMyTable)+1).Value = testDataItem 

cl1 = Sheet1.Cells(1,1) 
cl2 = Sheet1.Cells(len(myTable),len(myTable[0])+1) 

pivotSourceRange = Sheet1.Range(cl1,cl2) 
pivotSourceRange.Select() 

Sheet2 = wb.Sheets.Add (After=wb.Sheets (1)) 
Sheet2.Name = 'TCD' 

cl3=Sheet2.Cells(4,1) 
pivotTargetRange= Sheet2.Range(cl3,cl3) 
pivotTableName = 'tableauCroisé' 

pivotCache = wb.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=pivotSourceRange, Version=win32c.xlPivotTableVersion14) 

pivotTable = pivotCache.CreatePivotTable(TableDestination=pivotTargetRange, TableName=pivotTableName, DefaultVersion=win32c.xlPivotTableVersion14) 

pivotTable.PivotFields('Service').Orientation = win32c.xlRowField 
pivotTable.PivotFields('Service').Position = 1 
pivotTable.PivotFields('Region').Orientation = win32c.xlPageField 
pivotTable.PivotFields('Region').Position = 1 
pivotTable.PivotFields('Region').CurrentPage = 'IDF' 

dataField = pivotTable.AddDataField(pivotTable.PivotFields('CA')) 
dataField.NumberFormat = '# ### €' 

calculField = pivotTable.AddDataField(pivotTable.PivotFields('CA Bonifié')) 
calculField.NumberFormat = '# ### €' 

# wb.SaveCopyAs('C:/Users/Documents/Python/tcd.xlsx') 
# wb.Close(True) 
# Excel.Application.Quit() 
+0

你想添加配方'Cell.values'? – stovfl

+0

嗨@stovfl,我想要的是直接在數據透視表中進行計算。所以我不必在原始數據表中添加一列 – Yuva

+0

_「不必添加列」_,對我沒有意義嗎?[編輯]你的問題,並顯示預期的輸出圖片 – stovfl

回答

0

注意:我使用Sheet1作爲形象展示的所有相關指標及其容易驗證。
驗證後,您可以在稍後的步驟將Formula移動到數據透視表。

  1. STEP更換色譜柱EFormula =VLOOKUP
    參考:how-to-use-vlookup-match

    替換您的代碼如下:

    for row, testDataRow in enumerate(calField, 2): 
        #Sheet1.Cells(i+1,len(testMyTable)+1).Value = testDataItem 
        Sheet1.Cells(row, 5).Formula = '=VLOOKUP(A{}, H1:I5, MATCH(H1,H1:I1))'.format(row) 
    

    的結果應該顯示匹配Taux
    回來確認結果正常!

  2. STEP計算Taux