Python uses pyexcel to read and write Excel, CSV, TSV, HTML, JSON and other spreadsheets
Preparation work for environmental construction:
1. Install Python: Python is the foundation for running pyExcel, and you can download and install Python from the official website (Python. org).
2. Install pyExcel: You can use the pip command to install pyExcel, open a terminal or command line window, and run the following command: pip install pyExcel
Dependent class libraries:
1. pyexcel: It provides the function of reading and writing Excel, CSV, TSV, HTML, JSON and other spreadsheet files.
2. pyexcel io: Provides support for interacting with different file formats.
Complete sample code:
Firstly, we will demonstrate how to read and write Excel files, and then introduce how to handle other file formats.
python
#Import pyexcel module
import pyexcel as pe
#Read Excel file
sheet = pe.get_sheet(file_name='example.xlsx')
#Output the content of an Excel file
for row in sheet:
print(row)
#Write to Excel file
data = [["Name", "Age"], ["John", 25], ["Lisa", 30]]
sheet = pe.Sheet(data)
sheet.save_as("example_output.xlsx")
#Read CSV file
sheet = pe.get_sheet(file_name='example.csv')
#Output the content of a CSV file
for row in sheet:
print(row)
#Write CSV file
data = [["Name", "Age"], ["John", 25], ["Lisa", 30]]
sheet = pe.Sheet(data)
sheet.save_as("example_output.csv")
#Read TSV file
sheet = pe.get_sheet(file_name='example.tsv')
#Output the content of the TSV file
for row in sheet:
print(row)
#Write TSV file
data = [["Name", "Age"], ["John", 25], ["Lisa", 30]]
sheet = pe.Sheet(data)
sheet.save_as("example_output.tsv")
#Read HTML file
sheet = pe.get_sheet(file_name='example.html')
#Output the content of an HTML file
for row in sheet:
print(row)
#Write HTML file
data = [["Name", "Age"], ["John", 25], ["Lisa", 30]]
sheet = pe.Sheet(data)
sheet.save_as("example_output.html")
#Reading JSON files
sheet = pe.get_sheet(file_name='example.json')
#Output the content of the JSON file
for row in sheet:
print(row)
#Write JSON file
data = [["Name", "Age"], ["John", 25], ["Lisa", 30]]
sheet = pe.Sheet(data)
sheet.save_as("example_output.json")
Summary:
Using pyexcel, you can easily read and write a variety of spreadsheets, including Excel, CSV, TSV, HTML, JSON, and so on. It provides a simple and easy to use interface, which makes it easy and fast to process spreadsheet files.