python操作Excel

  • wlrd 读取excel表中的数据
  • xlwt 创建一个全新的excel文件,然后对这个文件进行写入内容以及保存
  • xlutils 读取一个excel文件,然后进行修改或追加,不能操作xlsx,只能读xls

读excel表

#导入模块
import xlrd
#打开excel文件
data = xlrd.open_workbook(r'C:\Users\907968\Desktop\test.xlsx')
table1 = data.sheets()[0] #通过索引顺序获取
table2 = data.sheet_by_index(0)  #通过索引顺序获取
table3 = data.sheet_by_name(u'Sheet1')  #通过名称获取
print(data1)
print(data2)
print(data3)

获取行数和列数

import xlrd
data = xlrd.open_workbook(r'C:\Users\907968\Desktop\test.xlsx')
table = data.sheets()[0]
nrows = table.nrows
ncols = table.ncols
print("行数:%d\n列数:%d"%(nrows,ncols))

获取整行或整列的值,以列表形式返回

rows = table.row_values(0)
cols = table.col_values(0)
print("rows:%s\ncols:%s"%(rows,cols))

获取单元格数据

cell_A1 = table.cell_value(0,0)
cell_C4 = table.cell_value(3,2)
print("A1:%s\nC4:%s"%(cell_A1,cell_C4))

写excel操作

import xlwt
#创建workbook
workbook = xlwt.Workbook(encoding='utf-8',style_compression=0)  #style...表示是否压缩,不常用

#创建一个sheet对象,一个sheet对象对应excel文件中的一张表格
sheet = workbook.add_sheet('test',cell_overwrite_ok=True)  #test为表名,cell...表示是否可以覆盖单元格,默认为false

#向表中添加数据
sheet.write(0,0,'EnglishName')  # 其中的'0-行, 0-列'指定表中的单元,'EnglishName'是向该单元写入的内容
txt1 = "中文名字"
sheet.write(0,1,txt1)

#保存
workbook.save(r'e:\test1.xls')

追加数据

import xlrd
import xlutils.copy
data = xlrd.open_workbook(r'C:\Users\907968\Desktop\test222.xls')
ws = xlutils.copy.copy(data)
table = ws.get_sheet(0)
table.write(0,3,"D1")
ws.save(r'C:\Users\907968\Desktop\test222.xls')

追加前:
A1 B1 C1
追加后:
A1 B1 C1 D1

插入多列

workbook = load_workbook(filename='这是一个表格.xlsx')
sheet = workbook.active
sheet.insert_cols(idx=2,amount=3)  #idx行索引位置,amount为数量
workbook.save(filename='这是一个表格.xlsx')

格子移动

workbook = load_workbook(filename='这是一个表格.xlsx')
sheet = workbook.active
sheet.move_range("C1:D4",rows=2, col=-2)  #C1:D4为框范围,rows为向下2格,..左两格
workbook.save(filename='这是一个表格.xlsx')

创建/删除新的sheet

workbook.creat_sheet('表格2')
workbook.remove(sheet)

冻结窗格

sheet.freeze_panes="G2"  #位置

添加筛选

sheet.auto_filter.ref = sheet.dimensions  #对整张表进行筛选

修改字体样式

from openpyxl.styles import Font
from openpyxl import load_workbook

workbook = load_workbook(filename='这是一个表格.xlsx')
sheet = workbook.active
cell = sheet['A1']
font = Font(name='思源黑体 Regular', size=12, bold=True,italic=True, color='FF0000')   #bold:是否加粗, italic 是否斜体
cell.font = font
workbook.save(filename='这是一个表格.xlsx')

设置对齐样式

Alignment(horizontal=水平对齐模式,vertical=垂直对齐模式,text_rottion=旋转角度,wrap_text=是否自动换行)

workbook = load_workbook(filename='这是一个表格.xlsx')
sheet = workbook.active
cell = sheet['A1']
alignment = Alignment(horizontal='center', vertical='center',text_rotation=45)
cell.alignment = alignment
workbook.save(filename='这是一个表格.xlsx')

水平对齐:distributed, justify, center, left, fill, centerContinuous, right, general
垂直对齐:bottom, distributed, justify, center, top

设置边框样式

side(style=边线样式,color=边线颜色)

Border(left=左边线样式,right=右边线样式,top=上边线样式,bottom=下边线样式

from openpyxl.styles import Side, Border
from openpyxl import load_workbook

workbook = load_workbook(filename='这是一个表格.xlsx')
sheet = workbook.active
cell = sheet['A3']
side = Side(style='thin', color='FF000000')
border = Border(left=side, right=side, top=side, bottom=side)
cell.border = border
workbook.save(filename='这是一个表格.xlsx')


边线样式:double, mediumDashDotDot, salantDashDot, dashDotDot, dotted, hair, mediumDashed, dashed, dashDot, thin, mediumDashDot, medium, thick

设置填充样式

PatternFill(fill_type=填充样式,fgCorlor=填充颜色)
GradientFill(stop=(渐变颜色1,渐变颜色2,...))

from openpyxl.styles import PatternFill, GrasientFill
from openpyxl import load_workbook

workbook = load_workbook(filename='这是一个表格.xlsx')
sheet = workbook.active
cell = sheet['A3']
pattern_fill = PatternFill(fill_type='solid', fgColor='99ccff')
cell_a3.fill = pattern_fill
cell = sheet['A4']
gradient_fill = GradientFill(stop=('FFFFFF','99ccff','000000'))
cell_a4.fill = grasient_fill
workbook.save(filename='这是一个表格.xlsx')

设置行高和列宽

row_dimensions[行编号].height=行高
column_dimensions[列编号].width=列宽

sheet.row_dimensions[1].height = 50
sheet.column_dimensions['B'].width = 20

合并单元格

merge_cells(待合并的格子编号)
merge_cells(start_row=起始行号,start_column=起始列号,end_row=结束行号,end_column=结束列号)

sheet.merge_cells('C1:D2')
sheet.merge_cells(start_row=7, start_column=1, end_row=8,end_column=4)

openpyxl插入图片

from openpyxl import load_workbook
from openpyxl.drawing.image import Image

workbook = load_workbook(filename='这是一个表格.xlsx')
sheet = workbook.active

logo = Image('每颗豆.png')
logo.height = 100
logo.width = 100

sheet.add_image(logo,'C1')
workbook.save(filename='这是一个表格.xlsx')

可视化分析

略,自查

你可能感兴趣的:(RPA,excel,python)