Opening Files with Different Formats

Opening Files with Different Formats

Aspose.Cells for Python via .NET allows developers to open spreadsheet files with different formats such as SpreadsheetML, Comma-separated values (CSV), Tab Delimited or Tab-separated values (TSV), ODS files. To open such files, developers can use the same methodology as they use for opening files of different Microsoft Excel versions.

Opening SpreadsheetML Files

SpreadsheetML files are XML representations of spreadsheets including all information about it, such as formatting, formulae etc. Since Microsoft Excel XP, an XML export option is added to Microsoft Excel that exports your spreadsheets to SpreadsheetML files.

from aspose.cells import LoadFormat, LoadOptions, 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(".")
# Opening SpreadsheetML Files
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions3 = LoadOptions(LoadFormat.SPREADSHEET_ML)
# Create a Workbook object and opening the file from its path
wbSpreadSheetML = Workbook(dataDir + "Book3.xml", loadOptions3)
print("SpreadSheetML file opened successfully!")

Opening HTML Files

Aspose.Cells for Python via .NET allows you to open HTML file into Workbook object. The HTML file should Microsoft Excel oriented i.e MS-Excel should be able to open it.

from aspose.cells import HtmlLoadOptions, LoadFormat, 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(".")
filePath = dataDir + "Book1.html"
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = HtmlLoadOptions(LoadFormat.HTML)
# Create a Workbook object and opening the file from its path
wb = Workbook(filePath, loadOptions)
# Save the MHT file
wb.save(filePath + "output.xlsx")

Opening CSV Files

Comma Separated Values (CSV) files contain records where the values are separated by commas. Data is stored as a table where each column is separated by the comma character and quoted by the double quote character. If a field value contains a double quote character it is escaped with a pair of double quote characters. You can also use Microsoft Excel to export spreadsheet data to CSV.

from aspose.cells import LoadFormat, LoadOptions, 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(".")
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions4 = LoadOptions(LoadFormat.CSV)
# Create a Workbook object and opening the file from its path
wbCSV = Workbook(dataDir + "Book_CSV.csv", loadOptions4)
print("CSV file opened successfully!")

Opening CSV files and replacing invalid characters

In Excel, when CSV file with special characters is opened, the characters are automatically replaced. The same is done by Aspose.Cells for Python via .NET API which is demonstrated in the code example given below.

from aspose.cells import LoadDataFilterOptions, LoadFilter, TxtLoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
filename = sourceDir + "[20180220142533][ASPOSE_CELLS_TEST].csv"
options = TxtLoadOptions()
options.separator = ';'
options.load_filter = LoadFilter(LoadDataFilterOptions.CELL_DATA)
options.check_excel_restriction = False
options.convert_numeric_data = False
options.convert_date_time_data = false
# Load CSV file
workbook = Workbook(filename, options)
print(workbook.worksheets[0].name)
print(len(workbook.worksheets[0].name))
print("CSV file opened successfully!")

Opening Tab Delimited Files

Tab delimited (Text) file contains spreadsheet data but without any formatting. Data is arranged in rows and columns like in tables and spreadsheets. Basically, a tab delimited file is a special kind of plain text file with a tab between each column.

from aspose.cells import LoadFormat, LoadOptions, 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(".")
# Opening Tab Delimited Files
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions5 = LoadOptions(LoadFormat.TAB_DELIMITED)
# Create a Workbook object and opening the file from its path
wbTabDelimited = Workbook(dataDir + "Book1TabDelimited.txt", loadOptions5)
print("Tab delimited file opened successfully!")

Opening Tab-Separated Values (TSV) Files

Tab-separated values (TSV) file contains spreadsheet data but without any formatting. It is the same with Tab Delimited file where data is arranged in rows and columns like in tables and spreadsheets.

from aspose.cells import LoadFormat, LoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = LoadOptions(LoadFormat.TSV)
# Create a Workbook object and opening the file from its path
workbook = Workbook(sourceDir + "SampleTSVFile.tsv", loadOptions)
# Using the Sheet 1 in Workbook
worksheet = workbook.worksheets[0]
# Accessing a cell using its name
cell = worksheet.cells.get("C3")
print("Cell Name: " + cell.name + " Value: " + cell.string_value)

Opening SXC Files

StarOffice Calc is similar to Microsoft Excel and supports formulas, charts, functions, and macros. The spreadsheets created with this software are saved with the SXC extension. The SXC file is also used for OpenOffice.org Calc spreadsheet files. Aspose.Cells for Python via .NET can read SXC files as demonstrated by the following code sample.

from aspose.cells import LoadFormat, LoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = LoadOptions(LoadFormat.SXC)
# Create a Workbook object and opening the file from its path
workbook = Workbook(sourceDir + "SampleSXC.sxc", loadOptions)
# Using the Sheet 1 in Workbook
worksheet = workbook.worksheets[0]
# Accessing a cell using its name
cell = worksheet.cells.get("C3")
print("Cell Name: " + cell.name + " Value: " + cell.string_value)

Opening FODS Files

FODS file is spreadsheet saved in OpenDocument XML without any compression. Aspose.Cells for Python via .NET can read FODS files as demonstrated by the following code sample.

from aspose.cells import LoadFormat, LoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = LoadOptions(LoadFormat.FODS)
# Create a Workbook object and opening the file from its path
workbook = Workbook(sourceDir + "SampleFods.fods", loadOptions)
print("FODS file opened successfully!")