python automation excel

Since there are a lot of data to be processed in the near future, repeated data operations are very cumbersome and meaningless. I plan to use python to deal with these things, so I will initially learn python to process tables.

Install the required libraries before learning:

pip install xlwt
pip install xlrd

1. Create excel and write data

First create an excel file, and create a workbook in it, and write data according to the index of the row and column.

import xlwt

#创建excel文件
wb = xlwt.Workbook()

#创建一个工作簿
ws = wb.add_sheet("summary")
#写入数据
ws.write(0,0,'test1')
ws.write(1,2,'test1')  #ws.write(row,col,value)   row/col都是从0开始

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

As a result, there is a data.xls file in the working folder. Open it to see what we wrote:

2. Read table data

Suppose there is a table file data.xls, which contains a workbook summary, as shown in the figure:

import xlrd

#打开文件
wb = xlrd.open_workbook('data.xls')

#查看文件中的工作簿个数和名字
ws_num = wb.nsheets
ws_names = wb.sheet_names()

print(f'文件中有{ws_num}个工作簿')
print(f'工作簿的名字为:{ws_names}')

#选择工作簿 
ws1 = wb.sheet_by_index(0)  #按照表格顺序读取第一个表格
ws2 = wb.sheet_by_name('summary')  #按照表格名称读取叫‘summary’的表格

#打印工作簿的行数和列数
print(f'sheet里面有{ws1.nrows}行{ws1.ncols}列的数据')

#获取单元格的数据 不同的方法获取
print(f'第一行第二列的值是:{ws1.cell_value(0,1)}')
print(f'第一行第二列的值是:{ws1.cell(0,1).value}')
print(f'第一行第二列的值是:{ws1.row(0)[1].value}')

#获取整行或整列数据
print(f'第一行的数据:{ws1.row_values(0)}')
print(f'第一列的数据是:{ws1.col_values(0)}')

 The output is:

3. Set the cell style

Simply set some cell styles, including height and width, alignment, border style and color, background color, etc. You can also set fonts, including font, font color, font size, bold, underline, italic, etc.

import xlwt
# import xlrd

wb = xlwt.Workbook()
ws = wb.add_sheet('data')

ft = xlwt.Font()

ft.name = '微软雅黑'  #设置字体
ft.colour_index = 2   #设置颜色
ft.height = 11*20     #设置字体大小(20属于单位)
ft.bold = True        #设置加粗
ft.underline = True   #设置下划线
ft.italic = True      #设置斜体

style = xlwt.XFStyle()
style.font = ft
ws.write(1, 1, '样式')
# ws.write(2, 2, '样式改变', style)

#设置单元格高度 第2行的高度(256是单位)
ws.row(2).height_mismatch = True  #设置此项才能改变单元格高度
ws.row(2).height = 10*256
#设置单元格宽度 第2列的高度(256是单位)
ws.col(2).width = 20*256

#设置对齐方式
alg = xlwt.Alignment()
alg.horz = 2               #设置水平对齐方式,1:左对齐,2:居中,3:右对齐
alg.vert = 1               #设置垂直对齐方式,0:上对齐,1:居中,3:下对齐

style.alignment = alg
# ws.write(2, 2, '样式改变', style)

#设置边框
border = xlwt.Borders()
#边框格式 1:细实线,2:粗实线,3:细虚线,4:中细虚线,
#5:大粗实线,6:双线,7:细点虚线,8:大粗虚线,9:细点划线,
#10:粗点划线,11:细双点划线,12:粗双点划线,13:斜点划线
border.left = 1
border.right = 2
border.top = 3
border.bottom = 4
#设置边框颜色,具体数字代表的颜色见下图
border.left_colour = 1  #白色
border.right_colour = 2  #红色
border.top_colour = 3    #绿色
border.bottom_colour = 4  #蓝色

style.borders = border

#设置背景颜色
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN  #设置背景颜色的模式是填实
pattern.pattern_fore_colour = 5

style.pattern = pattern


ws.write(2, 2, '样式改变', style)

wb.save('样式_test.xls')

The output is:

The correspondence between colors and numbers can refer to this picture:

Guess you like

Origin blog.csdn.net/panpan_jiang1/article/details/128402420