Create CSV Documents via Python
Native and high performance CSV (Comma Separated Values) creation programmatically using Python APIs.
Generating CSV file dynamically within running application is easy. In order to create CSV documents from scratch without requiring MS Office, we will use
API that offers different features for spreadsheets creation, manipulation and conversion using Python platform. Developers can easily enhance code for writing data, generating charts or graphs as well as creating table in spreadsheets.
How to Create CSV via Python
It is easy for the developers to create, load, modify and convert CSV (Comma Separated Values) within running different reporting applications for data processing in just a few lines of code.
- Import asposecells in your code file.
- Create Workbook class instance.
- Access the first worksheet of the workbook.
- Get the desired cell(s) of the worksheet and input the value into the cell(s).
- Use Save method to save the workbook as CSV file.
Aspose.Cells for Python via Java is platform-independent API and can be used on any platform (Windows, Linux and MacOS), just make sure that system have Java 1.8 or higher, Python 3.5 or higher.
- Install Java and add it to PATH environment variable, for example:
- Install Aspose.Cells for Python via Java from pypi, use command as:
$ pip install aspose-cells.
Following source code shows how to create a CSV file using Python.
import jpype import asposecells jpype.startJVM() from asposecells.api import Workbook, FileFormatType // Create Workbook object. workbook = Workbook(FileFormatType.CSV) // Access the first worksheet of the workbook. worksheet = workbook.getWorksheets().get(0) // Get the desired cell(s) of the worksheet and input the value into the cell(s). worksheet.getCells().get("A1").putValue("ColumnA") worksheet.getCells().get("B1").putValue("ColumnB") worksheet.getCells().get("A2").putValue("ValueA") worksheet.getCells().get("B2").putValue("ValueB") // Save the workbook as CSV file. workbook.save("output.csv") jpype.shutdownJVM()
CSV What is CSV File Format
Files with .csv (Comma Separated Values) extension represent plain text files that contain records of data with comma separated values. Each line in a CSV file is a new record from the set of records contained in the file. Such files are generated when data transfer is intended from one storage system to another. Since all applications can recognize records separated by comma, import of such data files to database is done very conveniently. Almost all spreadsheet applications such as Microsoft Excel or OpenOffice Calc can import CSV without much effort. Data imported from such files is arranged in cells of a spreadsheet for representation to user.Read More
Other Supported Spreadsheet Generation
You can also create other Microsoft Excel formats including few listed below.