学习记录:python xlrd xlwt openpyxl操作excel

实现的目标
1.创建excel表格
2.读取excel表格
3.读取单元格内容
4.修改单元格内容


安装库xlrd、xlwt

  1. xlrd是一个库,用于从Excel文件中以.xls格式读取数据和格式化信息
  2. xlwt是一个库,用于将数据和格式化信息写入较旧的Excel文件(例如:.xls)。
  • pip install xlrd
  • pip install xlwt

创建一个xls表格

  • encoding: 设置字符编码,一般要这样设置:xlwt.Workbook(encoding=‘utf-8’),就可以在excel中输出中文了,默认是ascii。
  • style_compression: 表示是否压缩,不常用。
  • cell_overwrite_ok: 表示是否可以覆盖单元格,默认值是False。
import xlrd ,xlwt

def create_excel():
    #创建excel对象
    book = xlwt.Workbook(encoding='utf-8',style_compression=0)
    #创建sheet表格
    sheet_1 = book.add_sheet('test1',cell_overwrite_ok=True)
    #保存.xls文件
    book.save(r'D:\code\test.xls')

create_excel()

创建表格创建多个表格时,再插入一行sheet代码就可以了,不过表格名不能重复,重复会报错

sheet_1 = book.add_sheet('test1',cell_overwrite_ok=True)
sheet_1 = book.add_sheet('test2',cell_overwrite_ok=True)

删除表格文件

import os
path = r'D:\code\test.xls'
if os.path.exists(path):
    os.remove(path)
else:
    print('文件不存在')

对单元格插入内容

  • sheet_1.write(0, 0, ‘用例编号’)
  • 表示为对A1插入内容为‘用例编号’
  • 0,0,即为A1,代码从0算起,所有A1即为 0,0,

例子:测试用例模板

import xlrd ,xlwt

def create_excel():
    #创建excel对象
    book = xlwt.Workbook(encoding='utf-8',style_compression=0)
    #创建sheet表格
    sheet_1 = book.add_sheet('test1',cell_overwrite_ok=True)

    #对单元格插入内容
    #横插入单元格
    sheet_1.write(0, 0, '用例编号')
    sheet_1.write(0, 1, '用例标题')
    sheet_1.write(0, 2, '前置条件')
    sheet_1.write(0, 3, '测试步骤')
    sheet_1.write(0, 4, '预期结果')
    sheet_1.write(0, 5, '实际结果')

    #竖插入单元格
    sheet_1.write(1, 0, 'case1')
    sheet_1.write(2, 0, 'case2')
    sheet_1.write(3, 0, 'case3')
    sheet_1.write(4, 0, 'case4')
    
    #保存.xls文件
    book.save(r'D:\code\test.xls')

create_excel()

对单元格插入长文本

    sheet_1.write(0, 0, r'''
    应是天仙狂醉,乱把白云揉碎
    ''')

raw字符串(原始字符串)
如果一个字符串包含很多需要转义的字符,对每一个字符都进行转义会很麻烦。为了避免这种情况,我们可以在字符串前面加个前缀r,表示这是一个 raw 字符串,里面的字符就不需要转义了。

获取行,列的总数,并且遍历获取的行数据内容

import xlrd ,xlwt

xlsfile = r'D:\code\test.xls'		# 读取.xls文件
book = xlrd.open_workbook(xlsfile)      # 得到Excel文件的book对象,实例化对象
sheet_0 = book.sheet_by_index(0)        # 通过sheet索引获得sheet对象
sheet_name = book.sheet_names()[0]      # 获得指定索引的sheet表名字
sheet1 = book.sheet_by_name(sheet_name) # 通过sheet名字来获取,当然如果知道sheet名字就可以直接指定
norows = sheet_0.nrows                  # 获取行总数
ncols = sheet_0.ncols                   # 获取列总数
for i in range(norows):                 # 循环打印每一行的内容
    print(sheet1.row_values(i)) 

通过坐标读取单元格的数据

import xlrd ,xlwt
xlsfile = r'D:\code\test.xls'
book = xlrd.open_workbook(xlsfile)      # 得到Excel文件的book对象,实例化对象
sheet_0 = book.sheet_by_index(0)        # 通过sheet索引获得sheet对象

#通过坐标读取单元格的数据
value1 = sheet_0.cell_value(0, 0)
value2 = sheet_0.cell_value(0, 1)
value3 = sheet_0.cell_value(1, 1)
print(value1)
print(value2)
print(value3)

安装openpyxl

pip install openpyxl -i https://mirrors.aliyun.com/pypi/simple/

创建表格,保存xlsx文件

from openpyxl import Workbook
wb = Workbook()

# ws1 = wb.create_sheet("Mysheet")  #默认最后一个
ws2 = wb.create_sheet("Mysheet", index=0) #第一个(工作簿位置)

# 保存
wb.save('test.xlsx')

读取表格,输出单元格的内容,修改单元格的内容,保存xlsx文件

from openpyxl import load_workbook  #导入模块
wb = load_workbook(filename = 'test.xlsx') #打开文件,默认可读写,若有需要可以指定write_only和read_only为True
sheet = wb['Mysheet'] #找到工作表
#print(sheet['B4'].value) #输出内容
sheet['B9'] = 2
wb.save('test.xlsx')

获取表格的最大行,最大列

print(sheet.max_row)
print(sheet.max_column)

获取工作簿名称,修改

from openpyxl import load_workbook  #导入模块
wb = load_workbook(filename = 'test.xlsx') #打开文件,默认可读写,若有需要可以指定write_only和read_only为True
sheet = wb['Mysheet'] #找到工作表

sheet.title = "123"
print(wb.sheetnames)
wb.save('test.xlsx')

猜你喜欢

转载自blog.csdn.net/qq_26086231/article/details/113794372