Different Ways to Save Files
Different Ways to Save Files
Aspose.Cells for Python via .NET provides the Workbook which represents a Microsoft Excel file and provides the properties and methods necessary to work with Excel files. The Workbook class provides the save method used to save Excel files. The save method has many overloads that are used to save files in different ways.
The file format that the file is saved to is decided by the SaveFormat enumeration
File Format Types | Description |
---|---|
CSV | Represents a CSV file |
Excel97To2003 | Represents an Excel 97 - 2003 file |
Xlsx | Represents an Excel 2007 XLSX file |
Xlsm | Represents an Excel 2007 XLSM file |
Xltx | Represents an Excel 2007 template XLTX file |
Xltm | Represents an Excel 2007 macro-enabled XLTM file |
Xlsb | Represents an Excel 2007 binary XLSB file |
SpreadsheetML | Represents a Spreadsheet XML file |
TSV | Represents a Tab-separated values file |
TabDelimited | Represents a Tab Delimited text file |
ODS | Represents an ODS file |
Html | Represents HTML file(s) |
MHtml | Represents an MHTML file(s) |
Represents a PDF file | |
XPS | Represents an XPS document |
TIFF | Represents Tagged Image File Format (TIFF) |
How to Save File to Different Formats
To save files to a storage location, specify the file name (complete with storage path) and the desired file format (from the SaveFormat enumeration) when calling the Workbook object’s save method.
from aspose.cells import SaveFormat, Workbook, XlsSaveOptions | |
# 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.xls" | |
# Load your source workbook | |
workbook = Workbook(filePath) | |
# Save in Excel 97 to 2003 format | |
workbook.save(dataDir + ".output.xls") | |
# OR | |
workbook.save(dataDir + ".output.xls", XlsSaveOptions()) | |
# Save in Excel2007 xlsx format | |
workbook.save(dataDir + ".output.xlsx", SaveFormat.XLSX) | |
# Save in Excel2007 xlsb format | |
workbook.save(dataDir + ".output.xlsb", SaveFormat.XLSB) | |
# Save in ODS format | |
workbook.save(dataDir + ".output.ods", SaveFormat.ODS) | |
# Save in Pdf format | |
workbook.save(dataDir + ".output.pdf", SaveFormat.PDF) | |
# Save in Html format | |
workbook.save(dataDir + ".output.html", SaveFormat.HTML) | |
# Save in SpreadsheetML format | |
workbook.save(dataDir + ".output.xml", SaveFormat.SPREADSHEET_ML) |
How to Save Workbook to Pdf
Portable Document Format (PDF) is a type of document created by Adobe back in 1990s. The purpose of this file format was to introduce a standard for representation of documents and other reference material in a format that is independent of application software, hardware as well as Operating System. The PDF file format has full capability to contain information like text, images, hyperlinks, form-fields, rich media, digital signatures, attachments, metadata, Geospatial features and 3D objects in it that can become as part of source document.
The following codes shows how to save workboook as pdf file With Aspose.Cells for Python via .NET:
from aspose.cells import PdfSaveOptions, Workbook | |
from aspose.cells.rendering import PdfCompliance | |
# Instantiate the Workbook object | |
workbook = Workbook() | |
# Set value to Cell. | |
workbook.worksheets[0].cells.get("A1").put_value("Hello World!") | |
workbook.save("pdf1.pdf") | |
# Save as Pdf format compatible with PDFA-1a | |
saveOptions = PdfSaveOptions() | |
saveOptions.compliance = PdfCompliance.PDF_A1A | |
workbook.save("pdfa1a.pdf") |
How to Save Workbook to Text or CSV Format
Sometimes, you want to convert or save a workbook with multiple worksheets into text format. For text formats (for example TXT, TabDelim, CSV, etc.), by default both Microsoft Excel and Aspose.Cells for Python via .NET save the contents of the active worksheet only.
The following code example explains how to save an entire workbook into text format. Load the source workbook which could be any Microsoft Excel or OpenOffice spreadsheet file (so XLS, XLSX, XLSM, XLSB, ODS and so on) with any number of worksheets.
When the code is executed, it converts the data of all sheets in the workbook to the TXT format.
You can modify the same example to save your file to CSV. By default, TxtSaveOptions.separator is comma, so do not specify a separator if saving to CSV format. Please note: If you are using the evaluation version and even if the TxtSaveOptions.export_all_sheets property is set to true, the program will still only export one worksheet.
from aspose.cells import TxtSaveOptions, 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(".") | |
# Load your source workbook | |
workbook = Workbook(dataDir + "book1.xls") | |
# Text save options. You can use any type of separator | |
opts = TxtSaveOptions() | |
opts.separator = '\t' | |
opts.export_all_sheets = True | |
# Save entire workbook data into file | |
workbook.save(dataDir + "out.txt", opts) |
How to Save File to Text Files with Custom Separator
Text files contain spreadsheet data without formatting. The file is a kind of plain text file that can have some customized delimiters between its data.
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(".") | |
filePath = dataDir + "Book11.csv" | |
# Instantiate Text File's LoadOptions | |
txtLoadOptions = TxtLoadOptions() | |
# Specify the separator | |
txtLoadOptions.separator = ',' | |
# Specify the encoding type | |
txtLoadOptions.encoding = "utf-8" | |
# Create a Workbook object and opening the file from its path | |
wb = Workbook(filePath, txtLoadOptions) | |
# Save file | |
wb.save(dataDir + "output.txt") |
How to Save Excel File to Html and Mht files
Aspose.Cells for Python via .NET can simply save an Excel file ,JSON, CSV or other files which could be loaded by Aspose.Cells for Python via .NET as .html and .mht files.
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Load your source workbook | |
workbook = Workbook("Book1.xlsx") | |
# save file to mhtml format | |
workbook.save("out.mht") |
How to Save Excel File to OpenOffice (ODS, SXC, FODS, OTS)
We can saving the files as open offce format : ODS, SXC, FODS, OTS etc.
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Load your source workbook | |
workbook = Workbook("book1.xlsx") | |
# Save as ods file | |
workbook.save("Out.ods") | |
# Save as sxc file | |
workbook.save("Out.sxc") | |
# Save as fods file | |
workbook.save("Out.fods") |
How to Save Excel File to JSON or XML
JSON (JavaScript Object Notation) is an open standard file format for sharing data that uses human-readable text to store and transmit data. JSON files are stored with the .json extension. JSON requires less formatting and is a good alternative for XML. JSON is derived from JavaScript but is a language-independent data format. The generation and parsing of JSON is supported by many modern programming languages. application/json is the media type used for JSON.
Aspose.Cells for Python via .NET supports saving files to JSON or XML.
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Load your source workbook | |
workbook = Workbook("Book1.xlsx") | |
# convert the workbook to json file. | |
workbook.save(dir + "book1.json") |