{"id":2151,"date":"2026-06-28T07:45:52","date_gmt":"2026-06-27T23:45:52","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2151"},"modified":"2026-03-28T08:54:05","modified_gmt":"2026-03-28T08:54:05","slug":"how-to-use-applicationnewworkbook-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationnewworkbook-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.NewWorkbook in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The Application.NewWorkbook property in xlwings provides a powerful way to create a new Excel workbook programmatically. It returns a Workbook object representing the newly created workbook, allowing for immediate manipulation of its contents, sheets, and properties. This functionality is essential for automating report generation, data processing workflows, or any task requiring the dynamic creation of Excel files without manual intervention.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The primary function of <code>Application.NewWorkbook<\/code> is to generate a fresh, blank workbook in Excel. This new workbook becomes the active workbook, and you can start adding data, formatting, or charts right away. It is particularly useful in scenarios where you need to produce multiple output files from a single data source or when building templates on the fly. Unlike simply opening an existing file, this method ensures you begin with a clean slate, adhering to default Excel settings unless otherwise modified.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax and Parameters<\/strong><br>In xlwings, you access this property through the <code>app<\/code> object, which represents the Excel Application. The basic syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>new_wb = app.books.add()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">While the VBA object model uses <code>Application.NewWorkbook<\/code>, xlwings employs the <code>books.add()<\/code> method as the direct equivalent. The <code>add()<\/code> method can accept several optional parameters to customize the new workbook:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Template<\/strong>: A string specifying the path to an Excel template file (.xltx, .xltm). If provided, the new workbook is based on this template instead of the default blank workbook.<\/li>\n\n\n\n<li><strong>Before<\/strong>: A Sheet object. The new workbook is placed before this specified sheet (relevant when adding within a specific workbook context, though typically used with <code>app.books.add()<\/code> for a new file).<\/li>\n\n\n\n<li><strong>After<\/strong>: A Sheet object. The new workbook is placed after this specified sheet.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For most common use cases, calling <code>app.books.add()<\/code> without arguments is sufficient. The method returns a <code>Book<\/code> object (xlwings&#8217; term for a Workbook), which you can assign to a variable for further operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Creating a Simple New Workbook and Adding Data:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Start a new Excel instance (visible=False for background operation)\napp = xw.App(visible=True)\n\n# Create a new workbook\nnew_workbook = app.books.add()\n\n# Access the first sheet and write data\nsheet = new_workbook.sheets&#91;0]\nsheet.range('A1').value = 'Product'\nsheet.range('B1').value = 'Sales'\nsheet.range('A2').value = &#91;'Widget A', 'Widget B', 'Widget C']\nsheet.range('B2').value = &#91;1500, 2100, 1850]\n\n# Save the workbook\nnew_workbook.save(r'C:\\Reports\\NewReport.xlsx')\n# new_workbook.close()\n# app.quit()<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Creating a Workbook from a Template:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\napp = xw.App(visible=False)\n\n# Specify the path to your template\ntemplate_path = r'C:\\Templates\\CompanyReport.xltx'\n\n# Create a new workbook based on the template\nnew_wb = app.books.add(template_path)\n\n# The new workbook already contains the template's formatting and sheets.\n# You can populate predefined cells.\nnew_wb.sheets&#91;'Data'].range('C5').value = 'Q4-2023'\nnew_wb.sheets&#91;'Summary'].range('B10').value = 95000\n\n# Save it with a new name\nnew_wb.save(r'C:\\Reports\\Q4_Report_Final.xlsx')\napp.quit()<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Creating Multiple Workbooks in a Loop:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\napp = xw.App(visible=False)\n\ndepartments = &#91;'Sales', 'Marketing', 'Engineering', 'HR']\n\nfor dept in departments:\n    # Create a new workbook for each department\n    wb = app.books.add()\n    wb.sheets&#91;0].name = dept\n    wb.sheets&#91;0].range('A1').value = f'Department: {dept}'\n    # ... add more department-specific data ...\n    wb.save(fr'C:\\DepartmentReports\\{dept}_Data.xlsx')\n    wb.close() # Close the workbook after saving to free memory\n\napp.quit()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Application.NewWorkbook property in xlwings provides a powerful way to create a new Excel workbo&#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-2151","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2151","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=2151"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2151\/revisions"}],"predecessor-version":[{"id":3288,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2151\/revisions\/3288"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}