📜  export_excel 文件 python (1)

📅  最后修改于: 2023-12-03 15:30:41.180000             🧑  作者: Mango

Exporting Excel Files in Python

Exporting Excel files is a common task for data analysts and developers, and Python provides several libraries to handle this task. In this article, we'll explore how to export Excel files in Python using three popular libraries: Pandas, Openpyxl, and Xlsxwriter.

Exporting Excel Files Using Pandas

Pandas is a popular data manipulation library that provides a simple way to export data frames to Excel files. Here's an example code snippet:

import pandas as pd

# Create a sample data frame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'Country': ['USA', 'Canada', 'UK']
})

# Export the data frame to an Excel file
df.to_excel('output.xlsx', index=False)

In this example, we first create a sample data frame that contains three columns: Name, Age, and Country. We then export this data frame to an Excel file using the to_excel() method. We provide the file name ('output.xlsx') and set the index parameter to False to exclude the index column from the export.

Exporting Excel Files Using Openpyxl

Openpyxl is a library for working with Excel files that supports both reading and writing. Here's an example code snippet that demonstrates how to export data to an Excel file:

from openpyxl import Workbook

# Create a sample data set
data = [
    ['Name', 'Age', 'Country'],
    ['Alice', 25, 'USA'],
    ['Bob', 30, 'Canada'],
    ['Charlie', 35, 'UK']
]

# Create a new workbook and sheet
wb = Workbook()
ws = wb.active

# Write the data to the worksheet
for row in data:
    ws.append(row)

# Save the workbook
wb.save('output.xlsx')

In this example, we define a sample data set as a list of lists with column headers in the first row. We then create a new workbook using the Workbook() method and add a new sheet using the active property. We write the data to the sheet using a for loop and the append() method. Finally, we save the workbook to disk using the save() method.

Exporting Excel Files Using Xlsxwriter

Xlsxwriter is a library for creating Excel files from scratch or modifying existing ones. Here's an example code snippet that demonstrates how to export data to an Excel file using Xlsxwriter:

import xlsxwriter

# Create a new workbook and sheet
workbook = xlsxwriter.Workbook('output.xlsx')
worksheet = workbook.add_worksheet()

# Add data to the worksheet
data = [
    ['Name', 'Age', 'Country'],
    ['Alice', 25, 'USA'],
    ['Bob', 30, 'Canada'],
    ['Charlie', 35, 'UK']
]

for row_num, row_data in enumerate(data):
    for col_num, col_data in enumerate(row_data):
        worksheet.write(row_num, col_num, col_data)

# Save the workbook
workbook.close()

In this example, we create a new workbook and sheet using the Workbook() and add_worksheet() methods. We then loop through the data and add it to the worksheet using the write() method. Finally, we save the workbook to disk using the close() method.

Which Library Should I Use?

All three libraries covered in this article are excellent choices for exporting Excel files in Python. Pandas is a good choice for exporting data frames, while Openpyxl is a good choice for creating or modifying existing Excel files. Xlsxwriter is a good choice for creating Excel files from scratch.

Ultimately, the choice of library will depend on your specific needs and preferences. Experiment with each library and choose the one that works best for your use case.