4 ways to create Excel in Python
Installation of the library
Library | use | Install |
---|---|---|
pandas | Excel creation | pip install pandas -i /simple/ |
openpyxl | Excel creation | pip install openpyxl -i /simple/ |
xlsxwriter | Excel creation | pip install xlsxwriter -i /simple/ |
xlwings | Excel creation | pip install xlwings -i /simple/ |
Code 1 - pandas
import pandas as pd import numpy as np def create_ecxel1(): # Create a DataFrame with 10 rows and 5 columns data = (10, 5) df = (data) # Save as Excel file df.to_excel('output_pandas.xlsx', index=False) create_ecxel1()
Code 2 - openpyxl
from openpyxl import Workbook def create_ecxel2(): # Create a workbook and worksheet wb = Workbook() ws = # Fill in 10 rows and 5 columns of data for row in range(1, 11): for col in range(1, 6): (row=row, column=col, value=row * col) # Save as Excel file ('output_openpyxl.xlsx') create_ecxel2()
Code 3 - xlsxwriter
import xlsxwriter def create_excel_with_10x5_data(): # Create a new Excel file and add a worksheet workbook = ('output_xlsxwriter.xlsx') worksheet = workbook.add_worksheet() # Define some formats (optional) bold = workbook.add_format({'bold': True}) # Bold format number_format = workbook.add_format({'num_format': '0.00'}) # Digital Format # Generate random data of 10 rows and 5 columns import random for row in range(10): #10 lines for col in range(5): #5 columns value = (1, 100) # Generate random numbers between 1 and 100 (row, col, value, number_format) # Write data and apply format # Close the workbook () # Call function to generate Excel filecreate_excel_with_10x5_data()
Code 4 - xlwings
import xlwings as xw import random def create_excel_with_xlwings(): # Create an Excel application instance and set it to invisible app = (visible=False) # Set visible=False to prevent the Excel interface from being opened wb = () # Create a new workbook sheet = ['Sheet1'] # Get the default worksheet # Generate random data of 10 rows and 5 columns data = [[(1, 100) for _ in range(5)] for _ in range(10)] # Write data to worksheet ('A1').value = data # Start writing data from A1 # Save the file ('output_xlwings.xlsx') () # Close the workbook () # Exit Excel app # Call function to generate Excel filecreate_excel_with_xlwings()
This is the end of this article about the 4 ways to create Excel in Python. For more related content related to Python creation Excel, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!
Related Articles
Django celery redis use specific practice
This article mainly introduces the specific practice of using django celery redis. The article introduces the example code in detail, which has certain reference learning value for everyone's study or work. Friends who need it, please learn with the editor below.2019-04-04Parse the sorted function in Python
This article mainly introduces the use of sorted functions in Python to parse. The sorted() function can sort iterable objects and can artificially specify the basis and method of sorting. This article provides solutions and some implementation codes. Friends who need it can refer to it.2023-10-10python test implementation method
Testing with python is also concise enough2008-12-12A detailed explanation of Python garbage collection in one article
This article mainly introduces a detailed explanation of Python garbage collection related information. Friends who need it can refer to it.2023-09-09A brief analysis of the example of calling Tkinter in Python
This article mainly introduces the example of Python calling Tkinter. By designing buttons in Python programs, users can easily call Python programs, thereby achieving the purpose of rapid, automated and efficient, and improving user experience and work efficiency2023-02-02python data extraction and split implementation code
This article mainly introduces the implementation code of Python data extraction and splitting. The example code is introduced in this article in detail, which has certain reference learning value for everyone's learning or work. Friends who need it, please learn with the editor below.2019-08-08Python implementation method to convert Excel into image
Today, the editor will share with you an article on how to convert Excel into image in Python. It has good reference value and hope it will be helpful to everyone. Let's take a look with the editor2018-10-103 ways to import or execute python source files in Python
This article mainly introduces three methods to import or execute Python source files. The python source code file has the extension "py" and can be explained. It can be run in the console. Friends who need it can refer to it.2023-08-08Detailed explanation of how to read and write CSV files in Python
CSV file (comma-separated value file) is a plain text file that uses a specific structure to arrange the data in the table. This article will introduce in detail the method of reading a write operation CSV file in Python. If you need it, please refer to it.2022-03-03Interpretation of branch and loop structure in Python
This article mainly introduces the interpretation of branch and loop structure in Python. In Python programming, branch (Branch) and loop (Loop) are one of the key elements to master. They allow you to execute different code blocks according to conditions and repeat specific tasks. Friends who need it can refer to it.2023-10-10