To write to an Excel file using Python, you can use the openpyxl
library. Here’s an example:
import openpyxl # create a new workbook workbook = openpyxl.Workbook() # select the active worksheet worksheet = workbook.active # write some data to the worksheet worksheet['A1'] = 'Name' worksheet['B1'] = 'Age' worksheet['A2'] = 'John' worksheet['B2'] = 30 worksheet['A3'] = 'Jane' worksheet['B3'] = 25 # save the workbook workbook.save('example.xlsx')
In this example, we first import the openpyxl
library. Then we create a new workbook using the Workbook()
function. We select the active worksheet using the active
attribute, and we write some data to it using the cell references (A1
, B1
, etc.). Finally, we save the workbook using the save()
method and specifying a filename.
You can customize this example to write any data you need to an Excel file.
Write Excel File Using xlsxwriter Module:
To write to an Excel file using the xlsxwriter
module, you can use the following code as a starting point:
import xlsxwriter # Create a new workbook and add a worksheet workbook = xlsxwriter.Workbook('example.xlsx') worksheet = workbook.add_worksheet() # Write some data to the worksheet worksheet.write('A1', 'Name') worksheet.write('B1', 'Age') worksheet.write('A2', 'John') worksheet.write('B2', 30) worksheet.write('A3', 'Jane') worksheet.write('B3', 25) # Save the workbook workbook.close()
In this example, we first import the xlsxwriter
module. Then we create a new workbook using the Workbook()
function and specify a filename. We add a new worksheet using the add_worksheet()
method. We then write some data to the worksheet using the write()
method and specifying the cell references and data. Finally, we save the workbook using the close()
method.
You can customize this example to write any data you need to an Excel file. The xlsxwriter
module provides many options for formatting cells and creating charts and graphs, so you can create professional-looking Excel files with Python.
Writing data to Excel files with xlwt:
To write data to an Excel file using the xlwt
module, you can use the following code as a starting point:
import xlwt # Create a new workbook and add a worksheet workbook = xlwt.Workbook(encoding="utf-8") worksheet = workbook.add_sheet('Sheet1') # Write some data to the worksheet worksheet.write(0, 0, 'Name') worksheet.write(0, 1, 'Age') worksheet.write(1, 0, 'John') worksheet.write(1, 1, 30) worksheet.write(2, 0, 'Jane') worksheet.write(2, 1, 25) # Save the workbook workbook.save('example.xls')
In this example, we first import the xlwt
module. Then we create a new workbook using the Workbook()
function and specify the encoding as utf-8
. We add a new worksheet using the add_sheet()
method and specify a name for the sheet. We then write some data to the worksheet using the write()
method and specifying the row, column, and data. Finally, we save the workbook using the save()
method and specifying a filename.
You can customize this example to write any data you need to an Excel file. The xlwt
module provides many options for formatting cells and creating charts and graphs, so you can create professional-looking Excel files with Python. However, note that xlwt
supports only the older Excel .xls
format, and not the newer .xlsx
format.
Writing Files with pyexcel:
To write to an Excel file using the pyexcel
module, you can use the following code as a starting point:
import pyexcel # Create a dictionary with data data = {"Name": ["John", "Jane"], "Age": [30, 25]} # Write the dictionary to an Excel file pyexcel.save_as(records=data, dest_file_name="example.xlsx")
In this example, we first import the pyexcel
module. Then we create a dictionary with the data that we want to write to the Excel file. Each key in the dictionary represents a column, and each value is a list of data for that column. We then use the save_as()
function to write the dictionary to an Excel file, specifying the filename as example.xlsx
.
pyexcel
supports a wide range of file formats, not just Excel, so you can use this module to write to CSV, TSV, JSON, and many other file types. To write to a different file format, simply change the destination file name and use the appropriate file extension for that format. You can also customize the data that you write to the file by modifying the dictionary that you pass to the save_as()
function.