Aspose.Cellsを使用したテキストを列に変換する
可能な使用シナリオ
Microsoft Excel を使用してテキストを列に変換できます。この機能は、データ タブのデータ ツール から利用できます。列の内容を複数の列に分割するには、データに特定の区切り記号(コンマなど)が含まれている必要があります。これにより、Microsoft Excel がセルの内容を複数のセルに分割します。Aspose.Cells for Python via .NET でも、この機能を Worksheet.Cells.text_to_columns() メソッドを介して提供しています。
Aspose.Cells for Python Excel ライブラリを使用して、テキストを列に変換する
次のサンプルコードは、Worksheet.Cells.text_to_columns()メソッドの使用法を説明しています。このコードでは、まず第1ワークシートの列Aに人名を追加します。名前はスペース文字で区切られています。その後、列AにWorksheet.Cells.text_to_columns()メソッドを適用し、出力エクセルファイルとして保存します。出力エクセルファイルを開くと、名前が列Aに、姓が列Bに表示されます。
サンプルコード
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") |