Python pywin32(win32com) Excel Pivot Table 操作備忘録

[はじめに]

Python pywin32(win32com) Excel 操作備忘録
の続編的な操作備忘録のピボットテーブル編です。

ノンプログラマーの素人が記述をしたコードです。
狭い利用範囲と少ない利用頻度での確認ですので、
記載内容に間違いや勘違いがあるかもしれません。
下記内容を参照の際は自己責任でお願い致します。

[動作確認環境]

Windows11
Excel2021

[操作備忘録]

csv
Type,ID,Name,Date,Count
0,220,nico,2022/2/20 0:00,1
0,220,nico,2022/2/21 0:00,2
0,220,nico,2022/2/22 0:00,3
1,221,nuke,2022/2/20 0:00,4
1,221,nuke,2022/2/21 0:00,5
1,221,nuke,2022/2/22 0:00,6
2,222,neko,2022/2/20 0:00,7
2,222,neko,2022/2/21 0:00,8
2,222,neko,2022/2/22 0:00,9
vba
'---------------
'Excel VBA
'---------------
Sub sample()

    '---------------
    '新しいシートの追加
    '---------------

    Dim ws As Worksheet
    Set ws = Sheets.Add

    '---------------
    'ピボットテーブルの作成
    '---------------

    'ピボットテーブルのキャッシュの作成
    Dim pcache As PivotCache
    Set pcache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="sample!A:E")

    'ピボットテーブルの作成
    Dim ptable As PivotTable
    Set ptable = pcache.CreatePivotTable(TableDestination:=ws.Range("A1"), TableName:="ピボットテーブル1")
    Debug.Print ptable.Name

    'ピボットテーブルに集計フィールドを挿入
    ptable.CalculatedFields.Add Name:="Twice", Formula:="=Count*2"

    '---------------
    'ピボットテーブルのフィールド
    '---------------

    'ピボットテーブルのフィルターの設定
    ptable.PivotFields("Type").Orientation = xlPageField

    'ピボットテーブルのフィールドの行の設定
    ptable.PivotFields("ID").Orientation = xlRowField
    ptable.PivotFields("Name").Orientation = xlRowField

    'ピボットテーブルのフィールドの列の設定
    ptable.PivotFields("Date").Orientation = xlColumnField

    'ピボットテーブルの"Count"を合計
    Dim pfield1 As PivotField
    Set pfield1 = ptable.AddDataField(Field:=ptable.PivotFields("Count"), Caption:="SumCount", Function:=xlSum)

    'ピボットテーブルの"Twice"を合計
    Dim pfield2 As PivotField
    Set pfield2 = ptable.AddDataField(Field:=ptable.PivotFields("Twice"), Caption:="SumTwice", Function:=xlSum)

    '---------------
    'ピボットテーブルの設定
    '---------------

    'ピボットテーブルの総計を非表示
    ptable.RowGrand = False
    ptable.ColumnGrand = False

    'ピボットテーブルのレポートのレイアウトを表形式で表示
    ptable.RowAxisLayout RowLayout:=xlTabularRow

    'ピボットテーブルのレポートのレイアウトをアイテムのラベルをすべて繰り返す
    ptable.RepeatAllLabels Repeat:=xlRepeatLabels

    'ピボットテーブルの小計を非表示
    ptable.PivotFields("ID").Subtotals(1) = True
    ptable.PivotFields("Name").Subtotals(1) = True
    ptable.PivotFields("Date").Subtotals(1) = True
    ptable.PivotFields("ID").Subtotals(1) = False
    ptable.PivotFields("Name").Subtotals(1) = False
    ptable.PivotFields("Date").Subtotals(1) = False

    'ピボットテーブルの"Type"の順序を降順に設定
    ptable.PivotFields("Type").AutoSort Order:=xlDescending, Field:="Type"

    'ピボットテーブルの"Type"の単一のアイテムを設定
    ptable.PivotFields("Type").ClearAllFilters
    ptable.PivotFields("Type").CurrentPage = "(All)"
    ptable.PivotFields("Type").CurrentPage = "0"
    ptable.PivotFields("Type").CurrentPage = "1"
    ptable.PivotFields("Type").CurrentPage = "2"
    ptable.PivotFields("Type").CurrentPage = "(blank)"

    'ピボットテーブルの"Type"の複数のアイテムを設定
    ptable.PivotFields("Type").ClearAllFilters
    ptable.PivotFields("Type").CurrentPage = "(All)"
    ptable.PivotFields("Type").EnableMultiplePageItems = True
    ptable.PivotFields("Type").PivotItems("0").Visible = True
    ptable.PivotFields("Type").PivotItems("1").Visible = True
    ptable.PivotFields("Type").PivotItems("2").Visible = True
    ptable.PivotFields("Type").PivotItems("(blank)").Visible = False

    'ピボットテーブルの"ID"の順序を昇順に設定
    ptable.PivotFields("ID").AutoSort Order:=xlAscending, Field:="ID"

    'ピボットテーブルの"ID"のフィルターの解除
    ptable.PivotFields("ID").ClearAllFilters

    'ピボットテーブルの"ID"を0より大きいに設定
    ptable.PivotFields("ID").PivotFilters.Add2 Type:=xlCaptionIsGreaterThan, Value1:=0

    'ピボットテーブルの"ID"を合計が10より大きいに設定
    ptable.PivotFields("ID").PivotFilters.Add2 Type:=xlValueIsGreaterThan, DataField:=ptable.PivotFields("SumCount"), Value1:=10

    'ピボットテーブルの"ID"の"220"を含まないよう設定(後述のスライサーで別の設定に上書きされます)
    ptable.PivotFields("ID").PivotItems("220").Visible = False

    'ピボットテーブルの"Name"の順序を降順に設定
    ptable.PivotFields("Name").AutoSort Order:=xlDescending, Field:="Name"

    'ピボットテーブルの"Name"のフィルターの解除
    ptable.PivotFields("Name").ClearAllFilters

    'ピボットテーブルの"Name"の"neko"を含まないよう設定(後述のスライサーで別の設定に上書きされます)
    ptable.PivotFields("Name").PivotItems("neko").Visible = False

    'ピボットテーブルの"Date"のフィルターの解除
    ptable.PivotFields("Date").ClearAllFilters

    'ピボットテーブルの"Date"の期間を設定(後述のタイムラインのスライサーで別の設定に上書きされます)
    ptable.PivotFields("Date").PivotFilters.Add2 Type:=xlDateBetween, Value1:="2022/1/1", Value2:="2022/6/30"

    'ピボットテーブルの"Date"を年と月でグループ化の設定
    ptable.PivotFields("Date").DataRange.Cells(1, 1).Group Start:=True, End:=True, Periods:=Array(False, False, False, False, True, False, True)

    'ピボットテーブルの"年"の小計を非表示
    ptable.PivotFields("年").Subtotals(1) = True
    ptable.PivotFields("年").Subtotals(1) = False

    '---------------
    'ピボットテーブルのスライサー
    '---------------

    'ピボットテーブルのスライサーのキャッシュの作成
    Dim scache1 As SlicerCache
    Set scache1 = ActiveWorkbook.SlicerCaches.Add2(ptable, SourceField:="ID")
    Debug.Print scache1.Name

    'ピボットテーブルのスライサーのキャッシュのフィルターの解除
    scache1.ClearAllFilters

    'ピボットテーブルのスライサーのキャッシュの設定
    scache1.SlicerItems("222").Selected = False

    'ピボットテーブルのスライサーの順序を昇順に設定
    scache1.SortItems = xlSlicerSortAscending

    'ピボットテーブルのスライサーの作成
    Dim slicer1 As Slicer
    Set slicer1 = scache1.Slicers.Add(ws, Name:="ID", Caption:="ID", Top:=0, Left:=300, Width:=100, Height:=100)

    'ピボットテーブルのスライサーの各種設定(作成時と同じ値なので変化無し)
    slicer1.Name = "ID"
    slicer1.Caption = "ID"
    slicer1.Top = 0
    slicer1.Left = 300
    slicer1.Width = 100
    slicer1.Height = 100

    '---------------
    'ピボットテーブルのスライサー
    '---------------

    'ピボットテーブルのスライサーのキャッシュの作成
    Dim scache2 As SlicerCache
    Set scache2 = ActiveWorkbook.SlicerCaches.Add2(ptable, SourceField:="Name")
    Debug.Print scache2.Name

    'ピボットテーブルのスライサーのキャッシュのフィルターの解除
    scache2.ClearAllFilters

    'ピボットテーブルのスライサーのキャッシュの設定
    scache2.SlicerItems("nico").Selected = False

    'ピボットテーブルのスライサーの順序を降順に設定
    scache2.SortItems = xlSlicerSortDescending

    'ピボットテーブルのスライサーの作成
    Dim slicer2 As Slicer
    Set slicer2 = scache2.Slicers.Add(ws, Name:="Name", Caption:="Name", Top:=0, Left:=400, Width:=100, Height:=100)

    'ピボットテーブルのスライサーの各種設定(作成時と同じ値なので変化無し)
    slicer2.Name = "Name"
    slicer2.Caption = "Name"
    slicer2.Top = 0
    slicer2.Left = 400
    slicer2.Width = 100
    slicer2.Height = 100

    '---------------
    'ピボットテーブルのタイムライン
    '---------------

    'ピボットテーブルのタイムラインのスライサーのキャッシュの作成
    Dim scache3 As SlicerCache
    Set scache3 = ActiveWorkbook.SlicerCaches.Add2(ptable, SourceField:="Date", Name:="NativeTimeline_Date", SlicerCacheType:=xlTimeline)
    Debug.Print scache3.Name

    'ピボットテーブルのタイムラインのスライサーのキャッシュのフィルターの解除
    scache3.ClearDateFilter

    'ピボットテーブルのタイムラインのスライサーのキャッシュの設定
    scache3.TimelineState.SetFilterDateRange StartDate:="2022/2/1", EndDate:="2022/3/31"

    'ピボットテーブルのタイムラインのスライサーの作成
    Dim slicer3 As Slicer
    Set slicer3 = scache3.Slicers.Add(ws, Name:="Date", Caption:="Date", Top:=0, Left:=500, Width:=300, Height:=100)

    'ピボットテーブルのタイムラインのスライサーの各種設定(作成時と同じ値なので変化無し)
    slicer3.Name = "Date"
    slicer3.Caption = "Date"
    slicer3.Top = 0
    slicer3.Left = 500
    slicer3.Width = 300
    slicer3.Height = 100

    '---------------
    'ピボットテーブルの更新
    '---------------

    '指定のピボットテーブルの更新
    pcache.Refresh

    '全部のピボットテーブルの更新
    ActiveWorkbook.RefreshAll

    '---------------
    'ホームポジションの選択
    '---------------

    ws.Select
    ws.Range("A1").Select

End Sub
pywin32
# coding:utf-8

import os

import win32com.client
import win32con
import win32gui


def main():

    # ------------------------------------------------------------------
    # Excelの定数を設定
    # ------------------------------------------------------------------
    # https://docs.microsoft.com/en-us/office/vba/api/excel(enumerations)
    # https://docs.microsoft.com/ja-jp/office/vba/api/excel(enumerations)
    # ------------------------------------------------------------------
    # Excelの定数を取得する方法もあるようです。
    # https://wacky.hatenadiary.com/entry/20091011/1255240572
    # ------------------------------------------------------------------
    # Excel Enum Constants
    # ------------------------------------------------------------------
    xlAbove = 0
    xlBelow = 1
    xlSolid = 1
    xlFirst = 0
    xlLast = 1
    xlLastCell = 11
    xlTopToBottom = 1
    xlLeftToRight = 2
    xlGeneral = 1
    xlAutomatic = -4105
    xlFormats = -4122
    xlNone = -4142
    xlCenter = -4108
    xlDistributed = -4117
    xlJustify = -4130
    xlBottom = -4107
    xlLeft = -4131
    xlRight = -4152
    xlTop = -4160
    xlRTL = -5004
    xlLTR = -5003
    xlContext = -5002
    # ------------------------------------------------------------------
    # Excel Enum XlPivotTableSourceType
    # ------------------------------------------------------------------
    xlConsolidation = 3
    xlDatabase = 1
    xlExternal = 2
    xlPivotTable = -4148
    xlScenario = 4
    # ------------------------------------------------------------------
    # Excel Enum XlPivotFieldOrientation
    # ------------------------------------------------------------------
    xlColumnField = 2
    xlDataField = 4
    xlHidden = 0
    xlPageField = 3
    xlRowField = 1
    # ------------------------------------------------------------------
    # Excel Enum XlConsolidationFunction
    # ------------------------------------------------------------------
    xlAverage = -4106
    xlCount = -4112
    xlCountNums = -4113
    xlDistinctCount = 11
    xlMax = -4136
    xlMin = -4139
    xlProduct = -4149
    xlStDev = -4155
    xlStDevP = -4156
    xlSum = -4157
    xlUnknown = 1000
    xlVar = -4164
    xlVarP = -4165
    # ------------------------------------------------------------------
    # Excel Enum XlLayoutRowType
    # ------------------------------------------------------------------
    xlCompactRow = 0
    xlOutlineRow = 2
    xlTabularRow = 1
    # ------------------------------------------------------------------
    # Excel Enum XlPivotFieldRepeatLabels
    # ------------------------------------------------------------------
    xlDoNotRepeatLabels = 1
    xlRepeatLabels = 2
    # ------------------------------------------------------------------
    # Excel Enum XlSortOrder
    # ------------------------------------------------------------------
    xlAscending = 1
    xlDescending = 2
    xlManual = -4135
    # ------------------------------------------------------------------
    # Excel Enum XlSlicerCacheType
    # ------------------------------------------------------------------
    xlSlicer = 1
    xlTimeline = 2
    # ------------------------------------------------------------------
    # Excel Enum XlSlicerSort
    # ------------------------------------------------------------------
    xlSlicerSortAscending = 2
    xlSlicerSortDataSourceOrder = 1
    xlSlicerSortDescending = 3
    # ------------------------------------------------------------------
    # Excel Enum XlPivotFilterType
    # ------------------------------------------------------------------
    xlBefore = 31
    xlBeforeOrEqualTo = 32
    xlAfter = 33
    xlAfterOrEqualTo = 34
    xlAllDatesInPeriodJanuary = 57
    xlAllDatesInPeriodFebruary = 58
    xlAllDatesInPeriodMarch = 59
    xlAllDatesInPeriodApril = 60
    xlAllDatesInPeriodMay = 61
    xlAllDatesInPeriodJune = 62
    xlAllDatesInPeriodJuly = 63
    xlAllDatesInPeriodAugust = 64
    xlAllDatesInPeriodSeptember = 65
    xlAllDatesInPeriodOctober = 66
    xlAllDatesInPeriodNovember = 67
    xlAllDatesInPeriodDecember = 68
    xlAllDatesInPeriodQuarter1 = 53
    xlAllDatesInPeriodQuarter2 = 54
    xlAllDatesInPeriodQuarter3 = 55
    xlAllDatesInPeriodQuarter4 = 56
    xlBottomCount = 2
    xlBottomPercent = 4
    xlBottomSum = 6
    xlCaptionBeginsWith = 17
    xlCaptionContains = 21
    xlCaptionDoesNotBeginWith = 18
    xlCaptionDoesNotContain = 22
    xlCaptionDoesNotEndWith = 20
    xlCaptionDoesNotEqual = 16
    xlCaptionEndsWith = 19
    xlCaptionEquals = 15
    xlCaptionIsBetween = 27
    xlCaptionIsGreaterThan = 23
    xlCaptionIsGreaterThanOrEqualTo = 24
    xlCaptionIsLessThan = 25
    xlCaptionIsLessThanOrEqualTo = 26
    xlCaptionIsNotBetween = 28
    xlDateBetween = 35
    xlDateLastMonth = 45
    xlDateLastQuarter = 48
    xlDateLastWeek = 42
    xlDateLastYear = 51
    xlDateNextMonth = 43
    xlDateNextQuarter = 46
    xlDateNextWeek = 40
    xlDateNextYear = 49
    xlDateThisMonth = 44
    xlDateThisQuarter = 47
    xlDateThisWeek = 41
    xlDateThisYear = 50
    xlDateToday = 38
    xlDateTomorrow = 37
    xlDateYesterday = 39
    xlNotSpecificDate = 30
    xlSpecificDate = 29
    xlTopCount = 1
    xlTopPercent = 3
    xlTopSum = 5
    xlValueDoesNotEqual = 8
    xlValueEquals = 7
    xlValueIsBetween = 13
    xlValueIsGreaterThan = 9
    xlValueIsGreaterThanOrEqualTo = 10
    xlValueIsLessThan = 11
    xlValueIsLessThanOrEqualTo = 12
    xlValueIsNotBetween = 14
    xlYearToDate = 52
    # ------------------------------------------------------------------

    # Excel起動
    xlApp = win32com.client.Dispatch("Excel.Application")

    # ExcelのWindow最大化
    win32gui.ShowWindow(xlApp.hwnd, win32con.SW_MAXIMIZE)

    # Excel表示
    xlApp.Visible = 1

    # Excelファイルオープン
    wb = xlApp.Workbooks.Open(f"{os.getcwd()}\\sample.csv")

    # Excelシートオブジェクト
    ws = wb.Worksheets(1)

    # ------------------------------------------------------------------

    # 指定したシートを選択
    ws.Activate()

    # シートの追加
    ws = xlApp.Worksheets.Add()

    # 指定したシートを選択
    ws.Activate()

    # ------------------------------------------------------------------
    # ピボットテーブルの作成
    # ------------------------------------------------------------------

    # ピボットテーブルのキャッシュの作成
    pcache = wb.PivotCaches().Create(SourceType=xlDatabase, SourceData="sample!A:E")

    # ピボットテーブルの作成
    ptable = pcache.CreatePivotTable(TableDestination=ws.Range("A1"), TableName="ピボットテーブル1")

    # ピボットテーブルに集計フィールドを挿入
    ptable.CalculatedFields().Add(Name="Twice", Formula="=Count*2")

    # ------------------------------------------------------------------
    # ピボットテーブルのフィールド
    # ------------------------------------------------------------------

    # ピボットテーブルのフィルターの設定
    ptable.PivotFields("Type").Orientation = xlPageField

    # ピボットテーブルのフィールドの行の設定
    ptable.PivotFields("ID").Orientation = xlRowField
    ptable.PivotFields("Name").Orientation = xlRowField

    # ピボットテーブルのフィールドの列の設定
    ptable.PivotFields("Date").Orientation = xlColumnField

    # ピボットテーブルのCountを合計
    pfield1 = ptable.AddDataField(Field=ptable.PivotFields("Count"), Caption="SumCount", Function=xlSum)

    # ピボットテーブルのTwiceを合計
    pfield2 = ptable.AddDataField(Field=ptable.PivotFields("Twice"), Caption="SumTwice", Function=xlSum)

    # ------------------------------------------------------------------
    # ピボットテーブルの設定
    # ------------------------------------------------------------------

    # ピボットテーブルの総計を非表示
    ptable.RowGrand = False
    ptable.ColumnGrand = False

    # ピボットテーブルのレポートのレイアウトを表形式で表示
    ptable.RowAxisLayout(RowLayout=xlTabularRow)

    # ピボットテーブルのレポートのレイアウトをアイテムのラベルをすべて繰り返す
    ptable.RepeatAllLabels(Repeat=xlRepeatLabels)

    # ピボットテーブルの小計を非表示
    ptable.PivotFields("ID").Subtotals = tuple(False for _ in range(12))
    ptable.PivotFields("Name").Subtotals = tuple(False for _ in range(12))
    ptable.PivotFields("Date").Subtotals = tuple(False for _ in range(12))

    # ピボットテーブルのTypeの順序を降順に設定
    ptable.PivotFields("Type").AutoSort(Order=xlDescending, Field="Type")

    # ピボットテーブルのTypeの単一のアイテムを設定
    ptable.PivotFields("Type").ClearAllFilters
    ptable.PivotFields("Type").CurrentPage = "(All)"
    ptable.PivotFields("Type").CurrentPage = "0"
    ptable.PivotFields("Type").CurrentPage = "1"
    ptable.PivotFields("Type").CurrentPage = "2"
    ptable.PivotFields("Type").CurrentPage = "(blank)"

    # ピボットテーブルのTypeの複数のアイテムを設定
    ptable.PivotFields("Type").ClearAllFilters
    ptable.PivotFields("Type").CurrentPage = "(All)"
    ptable.PivotFields("Type").EnableMultiplePageItems = True
    ptable.PivotFields("Type").PivotItems("0").Visible = True
    ptable.PivotFields("Type").PivotItems("1").Visible = True
    ptable.PivotFields("Type").PivotItems("2").Visible = True
    ptable.PivotFields("Type").PivotItems("(blank)").Visible = False

    # ピボットテーブルのIDの順序を昇順に設定
    ptable.PivotFields("ID").AutoSort(Order=xlAscending, Field="ID")

    # ピボットテーブルのIDのフィルターの解除
    ptable.PivotFields("ID").ClearAllFilters

    # ピボットテーブルのIDを0より大きいに設定
    ptable.PivotFields("ID").PivotFilters.Add2(Type=xlCaptionIsGreaterThan, DataField=None, Value1=0)

    # ピボットテーブルのIDを合計が10より大きいに設定
    ptable.PivotFields("ID").PivotFilters.Add2(Type=xlValueIsGreaterThan, DataField=ptable.PivotFields("SumCount"), Value1=10)

    # ピボットテーブルのIDの220を含まないよう設定
    ptable.PivotFields("ID").PivotItems("220").Visible = False

    # ピボットテーブルのNameの順序を降順に設定
    ptable.PivotFields("Name").AutoSort(Order=xlDescending, Field="Name")

    # ピボットテーブルのNameのフィルターの解除
    ptable.PivotFields("Name").ClearAllFilters

    # ピボットテーブルのNameのnekoを含まないよう設定
    ptable.PivotFields("Name").PivotItems("neko").Visible = False

    # ピボットテーブルのDateのフィルターの解除
    ptable.PivotFields("Date").ClearAllFilters

    # ピボットテーブルのDateの期間を設定
    ptable.PivotFields("Date").PivotFilters.Add2(Type=xlDateBetween, DataField=None, Value1="2022/1/1", Value2="2022/6/30")

    # ピボットテーブルのDateを年と月でグループ化の設定
    ptable.PivotFields("Date").DataRange.Cells(1, 1).Group(Start=True, End=True, By=None, Periods=tuple([False, False, False, False, True, False, True]))

    # ピボットテーブルの年の小計を非表示
    ptable.PivotFields("年").Subtotals = tuple(False for _ in range(12))

    # ------------------------------------------------------------------
    # ピボットテーブルのスライサー
    # ------------------------------------------------------------------

    # ピボットテーブルのスライサーのキャッシュの作成
    scache1 = wb.SlicerCaches.Add2(Source=ptable, SourceField="ID")

    # ピボットテーブルのスライサーのキャッシュのフィルターの解除
    scache1.ClearAllFilters

    # ピボットテーブルのスライサーのキャッシュの設定
    scache1.SlicerItems("222").Selected = False

    # ピボットテーブルのスライサーの順序を昇順に設定
    scache1.SortItems = xlSlicerSortAscending

    # ピボットテーブルのスライサーの作成
    slicer1 = scache1.Slicers.Add(SlicerDestination=ws)

    # ピボットテーブルのスライサーの各種設定
    slicer1.Name = "ID"
    slicer1.Caption = "ID"
    slicer1.Top = 0
    slicer1.Left = 300
    slicer1.Width = 100
    slicer1.Height = 100

    # ------------------------------------------------------------------
    # ピボットテーブルのスライサー
    # ------------------------------------------------------------------

    # ピボットテーブルのスライサーのキャッシュの作成
    scache2 = wb.SlicerCaches.Add2(Source=ptable, SourceField="Name")

    # ピボットテーブルのスライサーのキャッシュのフィルターの解除
    scache2.ClearAllFilters

    # ピボットテーブルのスライサーのキャッシュの設定
    scache2.SlicerItems("nico").Selected = False

    # ピボットテーブルのスライサーの順序を降順に設定
    scache2.SortItems = xlSlicerSortDescending

    # ピボットテーブルのスライサーの作成
    slicer2 = scache2.Slicers.Add(SlicerDestination=ws)

    # ピボットテーブルのスライサーの各種設定
    slicer2.Name = "Name"
    slicer2.Caption = "Name"
    slicer2.Top = 0
    slicer2.Left = 400
    slicer2.Width = 100
    slicer2.Height = 100

    # ------------------------------------------------------------------
    # ピボットテーブルのタイムライン
    # ------------------------------------------------------------------

    # ピボットテーブルのタイムラインのスライサーのキャッシュの作成
    scache3 = wb.SlicerCaches.Add2(Source=ptable, SourceField="Date", Name="NativeTimeline_Date", SlicerCacheType=xlTimeline)

    # ピボットテーブルのタイムラインのスライサーのキャッシュのフィルターの解除
    scache3.ClearDateFilter

    # ピボットテーブルのタイムラインのスライサーのキャッシュの設定
    scache3.TimelineState.SetFilterDateRange(StartDate="2022/2/1", EndDate="2022/3/31")

    # ピボットテーブルのタイムラインのスライサーの作成
    slicer3 = scache3.Slicers.Add(SlicerDestination=ws)

    # ピボットテーブルのタイムラインのスライサーの各種設定
    slicer3.Name = "Date"
    slicer3.Caption = "Date"
    slicer3.Top = 0
    slicer3.Left = 500
    slicer3.Width = 300
    slicer3.Height = 100

    # ------------------------------------------------------------------
    # ピボットテーブルの更新
    # ------------------------------------------------------------------

    # 指定のピボットテーブルの更新
    pcache.Refresh

    # 全部のピボットテーブルの更新
    wb.RefreshAll

    # ------------------------------------------------------------------

    # 指定したシートを選択
    ws.Activate()

    # 指定したシートを選択
    ws.Select()

    # A1セルを選択
    ws.Range("A1").Select()

    # ------------------------------------------------------------------

    # ブックを保存せずにクローズ
    wb.Close(False)

    # ------------------------------------------------------------------

    # Excel終了
    xlApp.Quit()

    # ------------------------------------------------------------------


if __name__ == "__main__":
    main()

[操作備忘録サンプル]

sample-csv
sample-txt
sample-py

[参考情報]

reference
https://learn.microsoft.com/ja-jp/office/vba/api/excel.pivotcaches
https://learn.microsoft.com/ja-jp/office/vba/api/excel.pivotcache
https://learn.microsoft.com/ja-jp/office/vba/api/excel.pivottables
https://learn.microsoft.com/ja-jp/office/vba/api/excel.pivottable
https://learn.microsoft.com/ja-jp/office/vba/api/excel.pivotfields
https://learn.microsoft.com/ja-jp/office/vba/api/excel.pivotfield
https://learn.microsoft.com/ja-jp/office/vba/api/excel.pivotfilters
https://learn.microsoft.com/ja-jp/office/vba/api/excel.pivotfilter
https://learn.microsoft.com/ja-jp/office/vba/api/excel.pivotfilter.filtertype
https://learn.microsoft.com/ja-jp/office/vba/api/excel.slicercaches
https://learn.microsoft.com/ja-jp/office/vba/api/excel.slicercache
https://learn.microsoft.com/ja-jp/office/vba/api/excel.slicers
https://learn.microsoft.com/ja-jp/office/vba/api/excel.slicer
https://learn.microsoft.com/ja-jp/office/vba/api/excel.range.group

enumeration
https://learn.microsoft.com/ja-jp/office/vba/api/excel.xlpivottablesourcetype
https://learn.microsoft.com/ja-jp/office/vba/api/excel.xlpivotfieldorientation
https://learn.microsoft.com/ja-jp/office/vba/api/excel.xlconsolidationfunction
https://learn.microsoft.com/ja-jp/office/vba/api/excel.xllayoutrowtype
https://learn.microsoft.com/ja-jp/office/vba/api/excel.xlpivotfieldrepeatlabels
https://learn.microsoft.com/ja-jp/office/vba/api/excel.xlsortorder
https://learn.microsoft.com/ja-jp/office/vba/api/excel.xlslicercachetype
https://learn.microsoft.com/ja-jp/office/vba/api/excel.xlslicersort
https://learn.microsoft.com/ja-jp/office/vba/api/excel.xlpivotfiltertype

hide subtotals of pivot table
https://stackoverflow.com/questions/71843882/#72016243

エクセルの神髄
https://excel-ubara.com/excelvba1/EXCELVBA393.html
https://excel-ubara.com/excelvba4/EXCEL244.html
https://excel-ubara.com/EXCEL/EXCEL915.html

雨のち晴れ
https://pvttbl.blog.fc2.com/