Common methods for reading and writing EXCEL files in Python

There are many ways to read and write Excel in Python. Different modules have slightly different ways of reading and writing. Here I will mainly introduce a few common ways.

  • Use xlrd and xlwt to read and write excel;
  • Use openpyxl to read and write excel;
  • Use pandas to read and write excel;

1. Data preparation

For the convenience of demonstration, I have created a new data.

2. 0 xlrd and xlwt

xlrd is a library for reading data and formatting information from Excel files in .xls format
xlwt is a library for writing data and formatting information to older Excel files (eg: .xls).

1 example
pip install xlrd
pip install xlwt

Let's start by reading the contents of the file

Next we come to writing. There are too many operations that can be performed for writing. I only list the commonly used operations here.

It should be noted that it is best to execute it through the command line in the current path, otherwise the file cannot be generated.

3. 0 openpyxl

openpyxl is a Python library for reading/writing Excel 2010 xlsx/xlsm/xltx/xltm files.
Installation package

pip install openpyx

After the installation is complete, you can start reading data.

Now let's start writing data

import openpyxl
import datetime
from openpyxl.styles import Font, colors, Alignment
#Instantiate
workbook = openpyxl.Workbook()
#Activate worksheet
sheet=workbook.active
#Write data
sheet['A1']='python'
sheet['B1 ']='javascript'
#Write time
sheet['A2'] = datetime.datetime.now().strftime("%Y-%m-%d")
# Row height of row 2
sheet.row_dimensions[2] .height = 40
# Column B column width
sheet.column_dimensions['B'].width = 30
# Set the data in A1 to be centered vertically and horizontally
sheet['A1'].alignment = Alignment(horizontal='center', vertical ='center')
# The following code specifies the isoline number 24, bold italics, and the font color is yellow. Directly use the font property of the cell and assign the Font object to it.
bold_itatic_24_font = Font(name='equiline', size=24, italic=True, color='00FFBB00', bold=True)
sheet['B1'].font = bold_itatic_24_font
# Merge cells and write data to the upper left corner Just
sheet.merge_cells('A2:B2') # Merge several cells in a row
# Split cells
# sheet.unmerge_cells('A2:B2')
#Save
workbook.save('new.xlsx')

4. 0 pandas

pandas supports xls, xlsx, xlsm, xlsb, odf, ods and odt file extensions for reading from the local file system or URL. Supports options for reading a single worksheet or a list of worksheets.
The first thing is still to install the package

pip install pandas

grammar:

pd.read_excel(io, sheet_name=0, header=0, names=None, index_col=None, usecols=None, squeeze=False,dtype=None, engine=None, converters=None, true_values=None, false_values=None, skiprows=None, nrows=None, na_values=None, parse_dates=False, date_parser=None, thousands=None, comment=None, skipfooter=0, convert_float=True, **kwds)

io, the storage path of Excel
sheet_name, the name of the worksheet to be read
header, which row to use as column names
, customize the final column name
index_col, the column used as
index usecols, which columns need to be read
squeeze, when the data is only Contains a column of
converters, enforces the column data type
skiprows, skips specific rows
nrows, the number of rows to be read
skipfooter, skips the last n rows
import pandas as pd
import os
file_path = os.path.dirname(os.path.abspath( __file__))
base_path = os.path.join(file_path, 'data.xlsx')
df = pd.read_excel(base_path)
print(df)

data input

语法:
DataFrame.to_excel(excel_writer, sheet_name='Sheet1', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, startrow=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='inf', verbose=True, freeze_panes=None)
参数说明:

excel_writer: File path or existing ExcelWriter
sheet_name: Name of the sheet that will contain the data file
na_rep: Missing data representation
float_format: String formatted as a floating point number. For example float_format = "%.2f" format is 0.1234 to 0.12.
columns: column
header: write the column name. If given a list of strings, it is assumed to be an alias for the column name.
index: Write row name (index)
index_label: Column label of the index column, if necessary. If not specified, and header and index are true, the index name is used. If the DataFrame uses multiple indexes, a sequence should be given.
startrow: The cell row in the upper left corner of the dump data frame.
startcol: The upper left corner cell column dumps the data frame.
engine: The engine "openpyxl" or "xlsxwriter" to use when writing. You can also set it via the options io.excel.xlsx.writer, io.excel.xls.writer and io.excel.xlsm.writer.
merge_cells: Writes multi-index and hierarchy rows into merged cells.
encoding: Encode the generated excel file. Only necessary for xlwt, other writers support unicode natively.
inf_rep: represents infinity.
verbose: Show more information in the error log.
freeze_panes: Specify the bottommost row and rightmost column to freeze
from pandas import DataFrame
data = {'name': ['Zhang San', 'Li Si', 'Wang Wu'],'age': [11, 12, 13],'sex': ['Male', 'Female', ' Male']}
df = DataFrame(data)
df.to_excel('file.xlsx')

After practicing 30 practical projects of interface automation testing in 7 days, 28K people joined the byte testing position. [Automated testing/interface testing/software testing/performance testing/Jmeter]

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!   

Guess you like

Origin blog.csdn.net/nhb687096/article/details/133205570