Import and export excel data for Python data processing

A major application of Python is data analysis, and in data analysis, we often encounter situations where Excel data needs to be processed. Here is a summary of Python processing Excel data, which is basically used in most cases. I believe that it will no longer be difficult to process Excel data with Python in the future!

xlwtLibrary to import data into Excel

Write data to an Excel file

wb = xlwt.Workbook()
# 添加一个表
ws = wb.add_sheet('test')


# 3个参数分别为行号,列号,和内容
# 需要注意的是行号和列号都是从0开始的
ws.write(0, 0, '第1列')
ws.write(0, 1, '第2列')
ws.write(0, 2, '第3列')

# 保存excel文件
wb.save('./test.xls')

As you can see, using xlwtthe library is very simple, basically three steps:

  1. Open an Workbookobject and add_sheetadd a table with methods
  2. Then use the writemethod to write data
  3. Finally savesave with method

It should be noted that xlwtthe rows and columns defined in the library are 0counted from the beginning

Customize Excel table styles

The table style generally has the following pieces of content: font, alignment, border, background color, width, and special content, such as hyperlinks, date and time, etc. Let's take a look at xlwthow to customize these styles with the library.

font

xlwtThe library supports many font attributes, which are as follows:

 

Setting the font requires the use xlwtof library XFStyleclasses and Fontclasses. The code template is as follows:

style = xlwt.XFStyle()

# 设置字体
font = xlwt.Font()
# 比如设置字体加粗和下划线
font.bold = True
font.underline = True
style.font = font

# 然后应用
ws.write(2, 1, 'test', style)

The settings of the subsequent properties are similar, and they are all 4 steps:

  1. getXFStyle
  2. Get the corresponding required properties, such as the Fontobject here
  3. Set specific property values
  4. The last thing is writeto apply it when the method writes the data.

cell alignment

First look at how to set the cell alignment

# 单元格对齐
alignment = xlwt.Alignment()

# 水平对齐方式和垂直对齐方式
alignment.horz = xlwt.Alignment.HORZ_CENTER
alignment.vert = xlwt.Alignment.VERT_CENTER
# 自动换行
alignment.wrap = 1
style.alignment = alignment

# 然后应用
ws.write(2, 1, 'test', style)

The above property of automatic line wrapping is quite useful, because we often have long data, it is best to use it together with the width property of the cell, so that the overall style will be much better

Cell width settings:

# 设置单元格宽度,也就是某一列的宽度
ws.col(0).width = 6666

cell background color

The attribute corresponding to the background color isPattern

# 背景色
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN

# 背景色为黄色
# 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta,
# 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow ,
# almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray
# ...
pattern.pattern_fore_colour = 5
style.pattern = pattern

# 然后应用
ws.write(2, 1, 'test', style)

cell border

The border property isBorders

There are 2 types of cell borders: color and border line style

You can set the color and style of the upper, lower, left and right borders respectively

# 边框
borders = xlwt.Borders()

# 边框可以分别设置top、bottom、left、right
# 每个边框又可以分别设置颜色和线样式:实线、虚线、无
# 颜色设置,其他类似
borders.left_colour = 0x40
# 设置虚线,其他类似
borders.bottom = xlwt.Borders.DASHED
style.borders = borders

# 然后应用
ws.write(2, 1, 'test', style)

Special content, such as hyperlinks and formulas

Special content generally encounters these categories: hyperlinks, formulas, and dates and times

To deal with these special contents, you need to useFormula

# 超链接
link = 'HYPERLINK("http://www.baidu.com";"Baidu")'
formula = xlwt.Formula(link)
ws.write(2, 0, formula)

# 公式也是类似
ws.write(1, 1, xlwt.Formula('SUM(A1,B1)'))

# 时间
style.num_format_str = 'M/D/YY'
ws.write(2, 1, datetime.datetime.now(), style)

The above is the whole content of writing data to Excel with Python. Let's take a look at how to read the data in Excel for processing.


xlrdLibrary to read data in Excel

Read Excel file

xlrdIt is also easy to read Excel data with the same library. Let’s first look at the implementation code

# 先打开一个文件
wb = xlrd.open_workbook(file_path)
# 获取第一个表
sheet1 = wb.sheet_by_index(0)

# 总行数
nrows = sheet1.nrows
# 总列数
ncols = sheet1.ncols

# 后面就通过循环即可遍历数据了
# 取数据
for i in range(nrows):
    for j in range(ncols):
        # cell_value方法取出第i行j列的数据
        value = sheet1.cell_value(i, j)
        print(value)

To sum up, it is divided into the following steps:

  1. First open the Excel file through xlrdthe library methodopen_workbook
  2. Then sheet_by_indexget the table by the method
  3. Then get the number of rows and columns of the table respectively, which is convenient for loop traversal later
  4. According to the number of columns and rows, loop through and cell_valueget the data in each cell through the method

Worksheet related operations

There are many ways to get a worksheet

# 通过索引
sheet1 = wb.sheets()[0]
sheet1 = wb.sheet_by_index(0)

# 通过名字
sheet1 = wb.sheet_by_name('test')

# 获取所有表名
# sheet_names = wb.sheet_names()

Get all data in a row or column

# 获取行中所有数据,返回结果是一个列表
tabs = sheet1.row_values(rowx=0, start_colx=0, end_colx=None)
# 返回一行一共有多少数据
len_value = sheet1.row_len(rowx=0)

row_valuesThe three parameters are: row number, start column and end column, where the end column Nonemeans to get all the data from the start column to the end

Similar to getting the data of a certain column

cols = sheet1.col_values(colx=0, start_rowx=0, end_rowx=None)

Processing time data

The time data is special, and it is not sent directly through the above cell_valuemethod. It needs to be converted to a timestamp first, and then formatted into the format we want.

2019/8/13 20:46:35For example, to get the time data in the format of Excel

# 获取时间
time_value = sheet1.cell_value(3, 0)

# 获取时间戳
time_stamp = int(xlrd.xldate.xldate_as_datetime(time_value, 0).timestamp())
print(time_stamp)

# 格式化日期
time_rel = time.strftime("%Y/%m/%d", time.localtime(time_stamp))
print(time_rel)

Basically there are three steps:

  1. cell_valueGet time value by method
  2. Then xldate_as_datetimeget the timestamp by the method
  3. then format it

Summarize

Excel files are a type of scenario that is often encountered when processing data with Python. With thexlwt help of and , Excel data can be imported and exported very quickly. xlrdYou can bookmark this article, and you can refer to it when dealing with Excel files in the future.

Today's content is shared here, and the editor will also give you a python spree [Jiajun Yang: 419693945] to help you learn better!

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324341066&siteId=291194637