{"id":2293,"date":"2026-09-07T07:22:25","date_gmt":"2026-09-06T23:22:25","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2293"},"modified":"2026-03-28T12:34:04","modified_gmt":"2026-03-28T12:34:04","slug":"how-to-use-worksheetsaveas-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetsaveas-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.SaveAs in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>SaveAs<\/code> method in the <code>Worksheet<\/code> object of the Excel object model is a powerful feature for saving a specific worksheet as a new workbook file. This is particularly useful in scenarios where you need to extract or archive a single sheet from a larger workbook, or when generating reports from a template. In xlwings, this functionality is accessed through the <code>api<\/code> property, which provides direct access to the underlying Excel object model, allowing for precise control similar to VBA.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The syntax for calling <code>SaveAs<\/code> via xlwings is: <code>worksheet.api.SaveAs(Filename, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AddToMru, TextCodepage, TextVisualLayout, Local)<\/code>. The key parameters are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Filename<\/code>: A string specifying the full path and name of the file to save. It is required.<\/li>\n\n\n\n<li><code>FileFormat<\/code>: A constant or number specifying the file format. Common values include <code>51<\/code> for <code>.xlsx<\/code> (Excel Workbook), <code>52<\/code> for <code>.xlsm<\/code> (Macro-Enabled Workbook), and <code>6<\/code> for <code>.csv<\/code> (CSV format). If omitted, the current file format is used.<\/li>\n\n\n\n<li><code>Password<\/code>: An optional string to set a password for opening the workbook.<\/li>\n\n\n\n<li><code>WriteResPassword<\/code>: An optional string to set a password for write reservation.<\/li>\n\n\n\n<li><code>CreateBackup<\/code>: If set to <code>True<\/code>, Excel creates a backup file.<\/li>\n\n\n\n<li>Other parameters like <code>ReadOnlyRecommended<\/code>, <code>AddToMru<\/code>, <code>TextCodepage<\/code>, <code>TextVisualLayout<\/code>, and <code>Local<\/code> are less frequently used and often left as default.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For example, to save the active worksheet as a new Excel workbook in the <code>.xlsx<\/code> format to a specific directory, you can use the following xlwings code. This example assumes you have an existing workbook with a worksheet object referenced. The code saves the worksheet &#8220;DataSheet&#8221; as a separate file, ensuring the original workbook remains unchanged. It demonstrates setting a filename, specifying the file format, and optionally adding a password for protection. This method is efficient for automating report generation or data extraction tasks, as it leverages Excel&#8217;s native saving capabilities through a clean Python interface.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel instance or open a workbook\napp = xw.apps.active\nwb = app.books&#91;'OriginalWorkbook.xlsx']\nws = wb.sheets&#91;'DataSheet']\n\n# Save the worksheet as a new workbook\nnew_file_path = r'C:\\Reports\\Extracted_DataSheet.xlsx'\nws.api.SaveAs(Filename=new_file_path, FileFormat=51) # 51 corresponds to .xlsx\n\n# To save as a CSV file without a password\ncsv_path = r'C:\\Reports\\DataSheet.csv'\nws.api.SaveAs(Filename=csv_path, FileFormat=6) # 6 corresponds to CSV\n\n# To save with a password for opening\nprotected_path = r'C:\\Reports\\Secure_DataSheet.xlsx'\nws.api.SaveAs(Filename=protected_path, FileFormat=51, Password='mysecret')<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `SaveAs` method in the `Worksheet` object of the Excel object model is a powerful feature for sa&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2293","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2293","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=2293"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2293\/revisions"}],"predecessor-version":[{"id":3497,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2293\/revisions\/3497"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}