Excel is a commonly used spreadsheet software, widely used in finance, business and education. It provides powerful data processing and analysis functions, can perform a variety of calculations and formula operations, and can create various types of charts and visualize data.Excel's flexibility makes it an important tool for processing and managing data. This article will introduce how to use for Python By CodeCreating Excel Filesas well asReading Excel data。
- Creating Excel Files with Python
- Reading Excel files using Python
Installing for Python Components
for Python is a third-party Python Excel component that can be used to create, manage, manipulate, and convert Excel documents on various Python platforms. To accomplish this task, you need to install for Python and plum-dispatch v1.7.4 into VS Code using the following pip command.
pip install -for-Python pip install plum-dispatch==1.7.4
The official website also provides a detailed installation tutorial, which you can refer to:How to Install for Python in VS Code
Creating Excel Files with Python
for Python provides a wide variety of classes, properties, and methods for developers to manipulate workbooks, worksheets, a range of cells, or even a specified cell. To create an Excel file and write specific data, refer to the following Python code.
from import * from import * # Create the Workbook object wb = Workbook() # Delete the default worksheet () # Add a worksheet and name it sheet = ("Sales sheet") # Merge cells A1:G1 ["A1:G1"].Merge() # Write data in A1 and set its cell style ["A1"].Text = "Sales Schedule" ["A1"].HorizontalAlignment = ["A1"].VerticalAlignment = ["A1"]. = True ["A1"]. = 13 # Set the row height of the first row [0].RowHeight = 30 # Write text and numbers to specified cells ["A2"].Text = "Order number" ["B2"].Text = "Date of order" ["C2"].Text = "Client Name" ["D2"].Text = "Product name" ["E2"].Text = "Order amount" ["F2"].Text = "Delivery date" ["G2"].Text = "Order Status" ["A3"].Text = "DXA-001" ["B3"].Text = "2022/12/1" ["C3"].Text = "Client 1" ["D3"].Text = "Product 1" ["E3"].Text = "5000" ["F3"].Text = "2022/12/12" ["G3"].Text = "Completed." ["A4"].Text = "DXA-002" ["B4"].Text = "2022/12/1" ["C4"].Text = "Client 2" ["D4"].Text = "Product 2" ["E4"].Text = "3000" ["F4"].Text = "2022/12/15" ["G4"].Text = "In progress." ["A5"].Text = "DXA-003" ["B5"].Text = "2022/12/18" ["C5"].Text = "Client 3" ["D5"].Text = "Product 3" ["E5"].Text = "1500" ["F5"].Text = "2022/12/28" ["G5"].Text = "Not completed." ["A6"].Text = "DXA-004" ["B6"].Text = "2022/12/19" ["C6"].Text = "Client 4" ["D6"].Text = "Product 4" ["E6"].Text = "2500" ["F6"].Text = "2022/12/24" ["G6"].Text = "Canceled" ["A7"].Text = "DXA-005" ["B7"].Text = "2022/12/20" ["C7"].Text = "Client 5" ["D7"].Text = "Product 5" ["E7"].Text = "5000" ["F7"].Text = "2022/1/5" ["G7"].Text = "In progress." # Set the row height of the specified row ["A2:G7"].RowHeight = 20 # Set the column width of the specified column (2, 11) (3, 9) (4, 8) (6, 11) (7, 10) # Set the border style for a specified range of cells ["A2:G7"].BorderAround() ["A2:G7"].BorderInside() ["A2:G2"].BorderAround() ["A2:G7"]. = # Set cell styles for a specified range of cells ["A2:G2"]. = Color.get_Gold() ["A2:G2"]. = True # Save the document ("Create", FileFormat.Version2016)
Generate files:
Reading Excel Data with Python
for Python provides Attributes can return the numeric value or text value of a specified cell as a string. To read data from an Excel worksheet, refer to the following Python code.
from import * from import * # Create the Workbook object wb = Workbook() # Load an Excel file ("Create"); # Get the first worksheet sheet = [0] # Get the cell area containing the data locatedRange = # Iterate through the rows and columns for i in range(len()): for j in range(len([i].Columns)): # Get the data of the cell print(locatedRange[i + 1, j + 1].Value + " ", end='') print("")
Returns results:
to this article on Python to create or read Excel files are introduced to this article, more related Python to read Excel files, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!