{"id":108,"date":"2026-03-10T11:41:25","date_gmt":"2026-03-10T03:41:25","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=108"},"modified":"2026-01-17T11:31:06","modified_gmt":"2026-01-17T11:31:06","slug":"how-to-refresh-a-pivot-table-using-xlwings","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-refresh-a-pivot-table-using-xlwings\/","title":{"rendered":"How To Refresh a Pivot Table Using xlwings?"},"content":{"rendered":"<h2>Method<\/h2>\n<p>def modify_data():<\/p>\n<p>\u00a0\u00a0\u00a0 #Modify data source<\/p>\n<p>\u00a0\u00a0\u00a0 sht_data.range(&#8216;F2&#8242;).value=&#8217;China&#8217;<\/p>\n<p>\u00a0<\/p>\n<p>def update():<\/p>\n<p>\u00a0\u00a0\u00a0 #Refresh pivot table<\/p>\n<p>\u00a0\u00a0\u00a0 pvt=sht_pvt.api.PivotTables(&#8216;Pivot&#8217;)<\/p>\n<p>\u00a0\u00a0\u00a0 pvt.RefreshTable()<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Refresh Pivot Table\n\nimport xlwings as xw    #Import xlwings\nimport os    #Import os\n\nroot = os.getcwd()    #Get current path\n#Create Excel application window, visible, no workbook added\napp=xw.App(visible=True, add_book=False)\n#Open data file, writable\nbk=app.books.open(fullname=root+r'\\Pivot.xlsx',read_only=False)\n#Get data source worksheet\nsht_data=bk.sheets.active\n#Data cell range\nrng_data=sht_data.api.Range('A1').CurrentRegion\n#Create a new worksheet for the pivot table\nsht_pvt=bk.sheets.add()\nsht_pvt.name='Pivot Table'\n    \ndef create_pivot_table():\n    #Location of data\n    rng_data=sht_data.api.Range('A1').CurrentRegion\n    #Location of pivot table\n    rng_pvt=sht_pvt.api.Range('A1')\n\n    #Create pivot table cache\n    pvc=bk.api.PivotCaches().Create(\\\n          SourceType=xw.constants.PivotTableSourceType.xlDatabase,\\\n          SourceData=rng_data)\n    #Create pivot table\n    pvt=pvc.CreatePivotTable(\\\n          TableDestination=rng_pvt,\\\n          TableName='Pivot')\n\n    #Set fields\n    pvt.PivotFields('Category').Orientation=\\\n        xw.constants.PivotFieldOrientation.xlPageField    #Page fields\n    pvt.PivotFields('Category').Position=1\n    pvt.PivotFields('Product').Orientation=\\\n        xw.constants.PivotFieldOrientation.xlColumnField    #Column fields\n    pvt.PivotFields('Product').Position=1\n    pvt.PivotFields('Origin').Orientation=\\\n        xw.constants.PivotFieldOrientation.xlRowField    #Row fields\n    pvt.PivotFields('Origin').Position=1\n    pvt.PivotFields('Amount').Orientation=\\\n        xw.constants.PivotFieldOrientation.xlDataField    #Value fields\n\ndef modify_data():\n    #Modify data source\n    sht_data.range('F2').value='China'\n\ndef update():\n    #Refresh pivot table\n    pvt=sht_pvt.api.PivotTables('Pivot')\n    pvt.RefreshTable()\n\n\ncreate_pivot_table()\nmodify_data()\nupdate()\n\n#bk.save()\n#bk.close()\n#app.kill()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"492\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/03\/image-16-1024x492.png\" alt=\"Refresh a Pivot Table Using xlwings\" class=\"wp-image-502\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/03\/image-16-1024x492.png 1024w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/03\/image-16-300x144.png 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/03\/image-16-768x369.png 768w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/03\/image-16.png 1051w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Method<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-xlwings-pivot-table"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":503,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/108\/revisions\/503"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}