{"id":7,"date":"2026-01-21T11:46:28","date_gmt":"2026-01-21T03:46:28","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=7"},"modified":"2026-01-17T04:17:22","modified_gmt":"2026-01-17T04:17:22","slug":"how-to-reference-activate-save-and-close-workbooks-using-xlwings","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-reference-activate-save-and-close-workbooks-using-xlwings\/","title":{"rendered":"How To Reference, Activate, Save, and Close Workbooks Using xlwings?"},"content":{"rendered":"<h2>Method<\/h2>\n<h3 style=\"margin: 0in; font-size: 14.0pt;\"><span lang=\"en-US\" style=\"font-weight: bold; font-family: Calibri;\">R<\/span><span lang=\"en-US\" style=\"font-weight: bold; font-family: 'Microsoft YaHei';\">eference<\/span><span lang=\"zh-CN\" style=\"font-weight: bold; font-family: \u5fae\u8f6f\u96c5\u9ed1;\"> a workbook<\/span><\/h3>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">bk=xw.books[0]<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#bk2=xw.books(2)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#pid=xw.apps.keys()<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#app2=xw.apps[pid[0]]<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 10.0pt;\">\u00a0<\/p>\n<h3 style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span style=\"font-weight: bold;\">Activate a workbook<\/span><\/h3>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xw.books(1).activate()<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xw.books.active.name<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings API<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#app.api.Workbooks(1).Activate()<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#app.api.ActiveWorkbook.Name<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 10.0pt;\">\u00a0<\/p>\n<h3 style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span style=\"font-weight: bold;\">Save the workbook<\/span><\/h3>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">bk.save()<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings API<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#bk=app.api.Workbooks(1)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#bk=app.api.Workbooks(&#8220;Workbook1&#8221;)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#bk.SaveAs(r&#8221;D:\\test.xlsx&#8221;)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#bk.SaveCopyAs(r&#8221;D:\\test.xlsx&#8221;)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 10.0pt;\">\u00a0<\/p>\n<h3 style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span style=\"font-weight: bold;\">Close the workbook<\/span><\/h3>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">bk.close()<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings API<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">app.api.Workbooks(1).Close()<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Reference, activate, save, and close a workbook\n\nimport xlwings as xw    #Import the xlwings package\n\napp=xw.App()\napp.books.add()\n\n#xlwings\nbk=xw.books&#91;0]\n#bk2=xw.books(2)\n\n#pid=xw.apps.keys()\n#app2=xw.apps&#91;pid&#91;0]]\n\n#Activate a workbook\n#xlwings\n#xw.books(1).activate()\n#xw.books.active.name\n\n#xlwings API\n#app.api.Workbooks(1).Activate()\n#app.api.ActiveWorkbook.Name\n\n#Save the workbook\n#xlwings\n#bk.save()\n\n#xlwings API\n#bk=app.api.Workbooks(1)\n#bk=app.api.Workbooks(\"Workbook1\")\n#bk.SaveAs(r\"D:\\test.xlsx\")\n#bk.SaveCopyAs(r\"D:\\test.xlsx\")\n\n#Close the workbook\n#xlwings\nbk.close()\n\n#xlwings API\n#app.api.Workbooks(1).Close()\n\napp.kill()<\/code><\/pre>\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":[3],"tags":[],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-xlwings-workbook"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/7","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=7"}],"version-history":[{"count":3,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":221,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions\/221"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}