Convert Text to Columns using Aspose.Cells

Possible Usage Scenarios

You can convert your Text to Columns using Microsoft Excel. This feature is available from Data Tools under the Data tab. In order to split the contents of a column to multiple columns, the data should contain a specific delimiter such as a comma (or any other character) based on which Microsoft Excel splits the contents of a cell to multiple cells. Aspose.Cells for Python via .NET also provides this feature via Worksheet.Cells.text_to_columns() method.

Convert Text to Columns using Aspose.Cells for Python Excel Library

The following sample code explains the usage of Worksheet.Cells.text_to_columns() method. The code first adds some people name in column A of the first worksheet. The first and last name is separated by space character. Then it applies Worksheet.Cells.text_to_columns() method on column A and save it as output excel file. If you open the output excel file, you will see, first names are in column A while last names are in column B as shown in this screenshot.

todo:image_alt_text

Sample Code

from aspose.cells import TxtLoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create a workbook.
wb = Workbook()
# Access first worksheet.
ws = wb.worksheets[0]
# Add people name in column A. Fast name and Last name are separated by space.
ws.cells.get("A1").put_value("John Teal")
ws.cells.get("A2").put_value("Peter Graham")
ws.cells.get("A3").put_value("Brady Cortez")
ws.cells.get("A4").put_value("Mack Nick")
ws.cells.get("A5").put_value("Hsu Lee")
# Create text load options with space as separator.
opts = TxtLoadOptions()
opts.separator = ' '
# Split the column A into two columns using TextToColumns() method.
# Now column A will have first name and column B will have second name.
ws.cells.text_to_columns(0, 0, 5, opts)
# Save the workbook in xlsx format.
wb.save(dataDir + "outputTextToColumns.xlsx")