{"id":2235,"date":"2026-08-09T07:28:21","date_gmt":"2026-08-08T23:28:21","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2235"},"modified":"2026-03-28T11:43:18","modified_gmt":"2026-03-28T11:43:18","slug":"how-to-use-workbooksopentext-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-workbooksopentext-in-the-xlwings-api-way\/","title":{"rendered":"How to use Workbooks.OpenText in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <strong>OpenText<\/strong> member of the <strong>Workbooks<\/strong> object in the Excel object model is a method used to import and parse a text file into a new Excel workbook. This is particularly useful for automating the loading of data from delimited text files (like CSV or TSV) or fixed-width text files directly into Excel without manual intervention. In xlwings, this functionality is accessed through the <code>api<\/code> property, which provides direct access to the underlying Excel object model, allowing precise control over the import process.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax in xlwings:<\/strong><br>The xlwings API call follows the pattern: <code>xlwings.Book.api.OpenText(...)<\/code>. However, since <code>OpenText<\/code> is a method of the Workbooks collection, it is typically used to create a new workbook. In xlwings, you can access it via the Excel application object. The general syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app = xw.App(visible=False) # Create an invisible Excel instance\napp.api.Workbooks.OpenText(Filename, ...)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>OpenText<\/code> method has numerous parameters to customize the import. Key parameters include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Filename<\/strong> (required, String): The full path and name of the text file to import.<\/li>\n\n\n\n<li><strong>Origin<\/strong>: Specifies the file origin (e.g., <code>xlWindows<\/code> for Windows or <code>xlMacintosh<\/code> for Mac). Often set to <code>xlWindows<\/code> (value 437) by default.<\/li>\n\n\n\n<li><strong>StartRow<\/strong> (Long): The starting row for parsing (default is 1).<\/li>\n\n\n\n<li><strong>DataType<\/strong> (XlTextParsingType): Sets how columns are parsed. Use <code>xlDelimited<\/code> (value 1) for delimited files (like CSV) or <code>xlFixedWidth<\/code> (value 2) for fixed-width files.<\/li>\n\n\n\n<li><strong>TextQualifier<\/strong> (XlTextQualifier): Specifies the text qualifier character, such as <code>xlTextQualifierDoubleQuote<\/code> (value 1) for double quotes.<\/li>\n\n\n\n<li><strong>ConsecutiveDelimiter<\/strong> (Boolean): <code>True<\/code> to treat consecutive delimiters as one.<\/li>\n\n\n\n<li><strong>Tab<\/strong>, <strong>Semicolon<\/strong>, <strong>Comma<\/strong>, <strong>Space<\/strong>, <strong>Other<\/strong>, <strong>OtherChar<\/strong>: Boolean parameters to set delimiters. For example, set <code>Comma=True<\/code> for CSV files. If <code>Other=True<\/code>, specify the character in <code>OtherChar<\/code>.<\/li>\n\n\n\n<li><strong>FieldInfo<\/strong> (Array): An array of arrays specifying the data type and width for each column. For delimited files, it often uses <code>xlGeneralFormat<\/code> (value 1). Example: <code>[[1, 1], [2, 1]]<\/code> sets the first two columns to general format.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><br>Here is an xlwings API code example that imports a comma-delimited CSV file, treating consecutive commas as one delimiter, and starting from the first row:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Start Excel in the background\napp = xw.App(visible=False)\n\n# Define the text file path\nfile_path = r'C:\\Data\\sales.csv'\n\n# Open the text file using OpenText\n# Parameters: Filename, StartRow=1, DataType=xlDelimited, Comma=True, ConsecutiveDelimiter=True\nworkbook = app.api.Workbooks.OpenText(\nFilename=file_path,\nOrigin=437, # xlWindows\nStartRow=1,\nDataType=1, # xlDelimited\nTextQualifier=1, # xlTextQualifierDoubleQuote\nConsecutiveDelimiter=True,\nComma=True,\nFieldInfo=&#91;&#91;1, 1], &#91;2, 1], &#91;3, 1]] # Set first three columns to general format\n)\n\n# Save the workbook as an Excel file\nworkbook.SaveAs(r'C:\\Data\\sales_imported.xlsx')\nworkbook.Close()\napp.quit()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The **OpenText** member of the **Workbooks** object in the Excel object model is a method used to im&#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-2235","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2235","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=2235"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2235\/revisions"}],"predecessor-version":[{"id":3417,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2235\/revisions\/3417"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}