Selecting multiple fields in win32c.xlPageField - python-3.x

Objective: I'm using a python script to generate an excel report (contains lots of pivot tables).
Problem: Unable to figure out how to add multiple items to pivot table filter
I've figured out a cumbersome solution where I can create individual data sets that are pre-filtered so I don't need to filter them in the pivot table. However, this isn't really efficient or effective if somebody wants to swap the filter on the final excel report.
I'll use code I found online as an example:
import win32com.client
Excel = win32com.client.gencache.EnsureDispatch('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],
['FR','Max' ,'M','Minus',-23.4567],
['CH','Max' ,'M','Plus',12.2314],
['SP','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()
wb.Worksheets.Add()
Sheet2 = wb.Worksheets("Sheet2")
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('Country').Orientation = win32c.xlPageField
PivotTable.PivotFields('Country').CurrentPage = 'SP'
PivotTable.PivotFields('Gender').Orientation = win32c.xlColumnField
PivotTable.PivotFields('Sign').Orientation = win32c.xlColumnField
DataField = PivotTable.AddDataField(PivotTable.PivotFields('Amount'))
Excel.Visible = 1
wb.SaveAs('ranges_and_offsets.xlsx')
Excel.Application.Quit()
This generates a pivot table in excel and sets the Country Filter to SP. I know adding the following line will enable the multi selection option.
PivotTable.PivotFields('Country').EnableMultiplePageItems = True
At this point, I'm stuck. I would like to find a way to set Country to both SP and DE. I feel like the correct way to do this is to switch .CurrentPage to .CurrentPageList
However, I can't seem to get .CurrentPageList to work
Any help would be highly appreciated!

this maybe too late for this post, but the way I solved this was creating two loops
PivotTable.PivotFields(target_column).Position = some_position
PivotTable.PivotFields(target_column).EnableMultiplePageItems = True
for item in show_values:
PivotTable.PivotFields(target_column).PivotItems(item).Visible = True
for item in excl_values:
PivotTable.PivotFields(target_column).PivotItems(item).Visible = False
I hope it helps!

Related

Openpyxl include a totalsrow that subtotals visible rows in Table

I am using openpyxl to create a Table. But I cannot figure out how to get the TotalsRow to appear and I cannot find any guidance through searching. Only the openpyxl documentation, which I cannot make sense of it. Here is the code I have:
LastRow = ws.max_row
tab = Table(displayName="Table10", ref="A1:CU198",
totalsRowShown=True)
style = TableStyleInfo(name="TableStyleMedium9", showFirstColumn=False,
showLastColumn=False, showRowStripes=False, showColumnStripes=True)
tblcolmn = TableColumn(id=1,name='TableTotals',totalsRowFunction='sum', totalsRowLabel='Totals')
tab.TableColumn = tblcolmn
tab.tableStyleInfo = style
ws.add_table(tab)
But it does not show the TotalsRow, so I cannot figure out what I am doing wrong. Oh, and I cannot calculate the sums in python, becuase I need the sum formula to change when I filter the Table.
I appreciate any help!
I couldn't figure out the solution with Openpyxl, but I did find the solution with xlsxwriter using the below code. Hope this helps someone:
ColDict = [{'header': DFColumnHeadersList,'total_string':'Totals','total_function':'sum' } for ColList in df.columns]
wb = xlsxwriter.Workbook(PathFileName)
ws = wb.add_worksheet('Sheet1')
ws.add_table(0,0,198,99,{'data': TestFileList, 'total_row': True, 'columns': ColDict,'last_column': False, 'banded_columns': True, 'name':'Table10'})
wb.close()

How to add Calculated Member to Pivot Table via Python win32

I'm trying to add a calculated column into this pivot table that is being generated via a python script with import win32com.client.
Code is posted below that is generating the pivot table. I'm not sure how to add a column. In VBA it would be wb.PivotTable.CalculatedField.Add but that didn't work (at least the syntax I tried).
I'm trying to calculate [OoCUnits] / [GrossUnits]
#Make Pivot version with four weeks data
Excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
win32c = win32com.client.constants
wb = Excel.Workbooks.Open(filename)
ws3 = wb.Worksheets('Dataset')
cl1 = ws3.Cells(1,1)
cl2 = ws3.Cells(max_row,max_col)
PivotSourceRange = ws3.Range(cl1,cl2)
ws3.Activate()
PivotSourceRange.Select()
wb.Sheets.Add (After=wb.Sheets(3))
ws4 = wb.Worksheets(4)
ws4.Name = 'Pivot'
cl3 = ws4.Cells(4,1)
PivotTargetRange = ws4.Range(cl3,cl3)
PivotTableName = 'OoCPivot'
#Make Pivot Table
PivotCache = wb.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=PivotSourceRange, Version=win32c.xlPivotTableVersion14)
PivotTable = PivotCache.CreatePivotTable(TableDestination=PivotTargetRange, TableName=PivotTableName, DefaultVersion=win32c.xlPivotTableVersion14)
PivotTable.PivotFields('Product Name').Orientation = win32c.xlRowField
PivotTable.PivotFields('Product Name').Position = 1
PivotTable.PivotFields('Customer Number').Orientation = win32c.xlPageField
PivotTable.PivotFields('Customer Number').Position = 1
PivotTable.PivotFields('Customer Name').Orientation = win32c.xlPageField
PivotTable.PivotFields('Customer Name').Position = 2
PivotTable.PivotFields('Week Ending Date').Orientation = win32c.xlColumnField
PivotTable.PivotFields('Week Ending Date').Position = 1
DataField = PivotTable.AddDataField(PivotTable.PivotFields('GrossUnits'))
DataField.NumberFormat = '#0.00'
DataField = PivotTable.AddDataField(PivotTable.PivotFields('OoCUnits'))
DataField.NumberFormat = '#0.00'
I'm inserting the values with the DataField. The script is not presenting any issues as is.
EDIT: The exact code I implemented to solve. The last two lines are just formatting. I'm including in case it helps someone else.
CalcField = PivotTable.CalculatedFields().Add('OoC Unit %','= OoCUnits / GrossUnits')
DataField = PivotTable.AddDataField(PivotTable.PivotFields('OoC Unit %'))
DataField.NumberFormat = '#%'
PivotTable.DisplayErrorString = True
Actually, the method is PivotTable.CalculatedFields().Add() according to docs. Consider placing below at bottom:
CalcField = PivotTable.CalculatedFields().Add("OC_GrossPct", "= OoCUnits / GrossUnits")
PivotTable.PivotFields("OC_GrossPct").Orientation = win32c.xlDataField

How to cancel auto filter on table with openpyxl

The title said it all :)
But still, I'm using the class Table from openpyxl.worksheet.table to define a table in excel file which I create. My problem is that the table that is created has Filter on the first row that I want to remove (from the script, not by opening the Excel file).
This is the calling for Table class:
tab = Table(displayName='Table_{}'.format(table_name.replace(' ', '_')),
ref="{}:{}".format(table_start, table_end))
This is what I get:
This is what I want to get:
I search for it at OpenPyXL Docs but find only adding that filtering...
There is any way to remove this?
Many thanks!
wb = load_workbook(filename="data.xlsx")
ws = wb["Sheet1"]
ws.auto_filter.ref = None
for i in range(2, ws.max_row + 1):
ws.row_dimensions[i].hidden = False
wb.save("data.xlsx")
I just ran across this and here is how I solved it.
#Opens the file
wb = load_workbook(filename = 'somefile')
#Set worksheet
ws = wb['sheetname'] # Tab name
#set table
tbl = ws.tables["tablename"]
#removes all filters
tbl.autoFilter.filterColumn.clear()
helo,
tab = Table( displayName='Table_{}'.format(table_name.replace(' ', '_'))
, ref="{}:{}".format(table_start, table_end)
, headerRowCount = 0 # default is 1
)

Creating an Excel Table with MATLAB

Since writing varying sized data ranges to a sheet seems to remove an Excel Table if the data range is larger than the existing Excel tables range, I want to create a Table in Excel every time I run the code. I'm currently having a fair bit of difficulty creating the tables. The code I have right now to try and create the ListObject:
eSheets = e.ActiveWorkbook.Sheets;
eSheet = eSheets.get('Item', j);
eSheet.Activate;
eSheet.Range(horzcat('A1:R',mat2str(size(obj,1)+1))).Select;
eSheet.Listobjects.Add;
eSheet.Listobjects.Item(1).TableStyle = 'TableStyleMedium2';
eSheet.ListObjects.Item(1).Name = tablename;
Any commentary or suggestions would be appreciated
I dont know about using eSheet in matlab but with the function
xlswrite(filename,A,sheet,xlRange)
you can also write your data from a matrix to an excel table http://de.mathworks.com/help/matlab/ref/xlswrite.html and with
[A,B] = xlsfinfo('foofoo.xlsx');
sheetValid = any(strcmp(B, 'foo2'));
you can also check if a table sheet already exist so that you wont override the old one and create a new one, as seen in https://de.mathworks.com/matlabcentral/answers/25848-how-to-check-existence-of-worksheet-in-excel-file
I am not sure if this is what you are looking for thougth
Alright, since the post got downvoted (not sure why...) I found my own answer with the help of some VBA forums and MATLAB Newsgroup. Here's what the final code looks like for anyone else that has issues:
e = actxserver('Excel.Application');
ewb = e.Workbooks.Open('Path/to/file');
eSheets = e.ActiveWorkbook.Sheets;
eSheet = eSheets.get('Item', j);
eSheet.Activate;
range = horzcat('A1:R',mat2str(size(obj,1)+1));
range_todelete = horzcat('A1:R',mat2str(size(obj,1)+300));
Range1 = eSheet.get('Range',range_todelete);
Range1.Value=[];
eSheet.Range(range).Select;
name = 'Table_Name';
try eSheet.ListObjects(name).Item(1).Delete
catch
end
eSheet.Listobjects.Add;
eSheet.ListObjects.Item(1).Name = name;
eSheet.ListObjects.Item(1).TableStyle = 'TableStyleMedium2';
Range = eSheet.get('Range',range);
Range.Value = cellarray;

Filling BAPI import table parameter in EXCEL using VBA

I have a query for me which requires to clear. I am using excel 2003. the sheet contains 12 columns. I need to do export data from excel to SAP. Before exporting I need to check if the record is exist or not, if exist then delete and insert.
I have two BAPIs for this one is import table, which needs to be filled the parameters, after filling this table the BAPI searches for the relevant records.
The list will be displayed in a table. I need to search that table with values from excel then import one field value to excel.
I write this code but, it is not working the BAPI giving Error 0.
Public Function Import_Order() As Boolean
Dim oBAPIGetOrder As Object
Dim oBAPIVariant1 As Object
Dim oBAPIVariant2 As Object
Dim oBAPIVariant3 As Object
Dim oBAPIImpOrder As Variant
Dim oBAPIRet As Boolean
Dim oDoNothing As Variant
gBAPIPlanOrder = 0
Set oBAPIGetPlOrder = sBAPIControl.Add("PLANED_GET_DET_LIST") 'BAPI
Set oBAPIVariant1 = oBAPIGetPlOrder.exports.Item("SELECTIONCRITERIA") 'Internal table
Set oBAPIVariant2 = oBAPIGetPlOrder.Tables.Item("DETAILEDLIST") 'Table
oBAPIVariant1.Value("MATERIAL") = eMaterial
oBAPIVariant1.Value("PLANT") = ePlnPlant
lBAPIRet = oBAPIGetPlOrder.call
If lBAPIRet Then
'oBAPIImpOrder = oBAPIGetPlOrder.imports.Item("PLANNEDORDER_NUM")
a = oBAPIVariant2.Rows.Count
oBAPIImpOrder = oBAPIVariant2.Value("PLANNEDORDER_NUM")
Import_PlannedOrder = True
Else
oBAPIImpOrder = 0
Import_PlannedOrder = False
End If
End Function
Thanks in advance for any help...
please place the call function statement
lBAPIRet = oBAPIGetPlOrder.call
after directly the exports statement and before the tables and importstatements

Resources