Opening TXT files using preferred parser then paste to Excel using Python via Java

Hi, I stumbled across this post: Convert CSV, TSV and TXT to Excel|Documentation and was wondering if this can also be done using Aspose Python via Java, if yes, appreciate if you could provide me the code or link with the exact function to the link provided, thank you.

@myDAN

Yes, you can open TXT files using a preferred parser and then paste the data into Excel using Aspose Python via Java. The Aspose.Cells library provides functionality to handle various text formats, including CSV, TSV, and TXT files, allowing you to specify custom parsing options.

Here’s a sample code snippet that demonstrates how to use the preferred parser for opening a TXT file and saving it to an Excel file:

import jpype
import jpype.imports
from jpype.types import JString

# Start the JVM
jpype.startJVM()

# Import Aspose.Cells classes
from com.aspose.cells import Workbook, TxtLoadOptions, LoadFormat

# Specify the path to your TXT file
txt_file_path = "path/to/your/file.txt"
output_excel_path = "path/to/output/file.xlsx"

# Create TxtLoadOptions and set the preferred parser
load_options = TxtLoadOptions(LoadFormat.TXT)
# You can set preferred parsers here if needed
# load_options.setPreferredParsers(...)

# Load the TXT file into a Workbook
workbook = Workbook(txt_file_path, load_options)

# Save the Workbook to an Excel file
workbook.save(output_excel_path)

# Shutdown the JVM
jpype.shutdownJVM()

In this code:

  • We start the Java Virtual Machine (JVM) to use Aspose.Cells.
  • We create an instance of TxtLoadOptions to specify how the TXT file should be parsed.
  • The Workbook class is used to load the TXT file with the specified options.
  • Finally, we save the workbook as an Excel file.

Make sure to adjust the paths to your TXT file and the desired output Excel file accordingly. You can also customize the load_options to set specific preferred parsers based on your requirements.

For more detailed information, you can refer to the official documentation on converting CSV, TSV, and TXT files to Excel here.

Sources:
[1]: Convert CSV, TSV and TXT to Excel - Aspose Documentation

1 Like

@myDAN
Please try the following sample code to convert CSV file to XLSX format:

import jpype
import asposecells
jpype.startJVM()

# Import Aspose.Cells classes
from asposecells.api import Workbook, TxtLoadOptions, LoadFormat, CellsHelper

print(CellsHelper.getVersion())

# Create TxtLoadOptions
load_options = TxtLoadOptions(LoadFormat.CSV)

# Load the CSV file into a Workbook
workbook = Workbook("sample.csv", load_options)

# Save the Workbook to an Excel file
workbook.save("sample_out.xlsx")

# Shutdown the JVM
jpype.shutdownJVM()

Hope helps a bit.

1 Like

This is what I’m using now, thanks for the sample codes

import jpype
import jpype.imports
from jpype.types import String
import asposecells

jpype.startJVM()

from asposecells.api import Workbook, TxtLoadOptions, LoadFormat, Encoding

txt_file_path = insert path here
output_excel_path = insert path here

load_options = TxtLoadOptions()
load_options.setSeparator(‘,’)
load_options.setEncoding(Encoding.getUTF8())
load_options.setConvertDateTimeData(True)

workbook = Workbook(txt_file_path, load_options)

workbook.save(output_excel_path)

jpype.shutdownJVM()

@myDAN
By using the following sample code for testing, we can obtain the correct results. result.zip (7.1 KB)

import jpype
import asposecells

jpype.startJVM()

from asposecells.api import Workbook, TxtLoadOptions, LoadFormat, Encoding

txt_file_path = "sample.csv"
output_excel_path = "sample_python_out.xlsx"

load_options = TxtLoadOptions()
load_options.setSeparator(',')
load_options.setEncoding(Encoding.getUTF8())
load_options.setConvertDateTimeData(True)

workbook = Workbook(txt_file_path, load_options)

workbook.save(output_excel_path)

jpype.shutdownJVM()

If you still have questions, please provide your sample file. We will check it soon.

1 Like