最近写python脚本经常需要操作excel文件,记录一下。
xlrd、xlwt模块
- 特点:支持xlsx和xls两种格式,适合操作2003版本之前的数据
xlutils模块 #主要是用该模块的copy方法,避免覆盖源文件,配合xlrd和xlwt使用
openpyxl模块
- 特点只支持xlsx格式,适合操作2007版之后的数据
下载方法一致 pip install 模块名
操作基本步骤一致:
- 开指定的excel文件,返回一个data对象
- 通过data对象可以得到各个sheet对象
- 通过sheet对象可以获取各个单元格,每个单元格是一个cell对象
- 最后写入数据然后保存
代码以openpyxl为例
from openpyxl import load_workbook
# 1.读取表格
# 1.1workbook
wb = load_workbook(file_path_origin)
# 1.2sheet
# sheet_names = wb.sheetnames
# print(sheet_names) #返回一个列表
sheet_mens = wb.get_sheet_by_name('男装') # 获取指定sheet页
# print(sheet_mens) #<Worksheet "男装">
# 1.3cell单元格的值 ABCDEFG表示第1234567列
# print(sheet_mens['C6'].value) # 第3列6行单元格的值
# 2.写入表格数据
# sheet_mens['C6'].value = "啦啦啦"# 将本sheet页第3列6行单元格的值改为啦啦啦
# # 3.保存
# new_path = "\2020-03-24-C6"
# wb.save(common_path + new_path + ".xlsx") # 写入后保存到新的表格,将原表格的数据全部复制出来并按照原格式保存
# 获取指定固定列指定行数据
for i in range(3,10): #range不包含10
cellvalue = ws.cell(row=i,column=3).value
print(cellvalue)
for col in ws.iter_cols( min_col=3, max_col=3, min_row=3, max_row=10): #包含最大值
for cell in col:
print(cell.value)
#获取整列数据
rows = sheet_mens.max_row #该sheet页最大行数
columns = sheet_mens.max_column #该sheet页最大列数
print(columns)
columndata = []
rowsdata = []
for i in range(1, rows + 1):
cellvalue = sheet_mens.cell(row=i,column=3).value
columndata.append(cellvalue)
print(f'第3列的所有值为:{columndata}')
# 获取整行数据
for i in range(1,columns+1):
rowsvalue = sheet_mens.cell(row=4,column=i).value
rowsdata.append(rowsvalue)
print(f"第4行的所有值为:{rowsdata}")
第二段代码
from openpyxl import *
common_path = r"D:\Work\爬虫-八爪鱼"
file_path = r"\2020-02-16-clean -男装销量前80"
file_path_origin = common_path + file_path + ".xlsx"
new_path = '\2020-03-24-duixiang'
file_path_new = common_path+new_path+".xlsx"
class Excel(object):
def __init__(self, file):
self.file = file_path_origin
self.new_file = file_path_new
self.wb = load_workbook(self.file)
sheets = self.wb.get_sheet_names()
self.sheet = sheets[0]
self.ws = self.wb[self.sheet]
# 获取表格的总行数和总列数
def getRowsClosNum(self):
rows = self.ws.max_row
columns = self.ws.max_column
return rows, columns
# 获取某个单元格的值
def getCellValue(self, row, column):
cellvalue = self.ws.cell(row=row, column=column).value
return cellvalue
# 获取某列的所有值
def getColValues(self, column):
rows = self.ws.max_row
columndata = []
for i in range(1, rows + 1):
cellvalue = self.ws.cell(row=i, column=column).value
columndata.append(cellvalue)
return columndata
# 获取某行所有值
def getRowValues(self, row):
columns = self.ws.max_column
rowdata = []
for i in range(1, columns + 1):
cellvalue = self.ws.cell(row=row, column=i).value
rowdata.append(cellvalue)
return rowdata
# 设置某个单元格的值
def setCellValue(self, row, colunm, cellvalue):
try:
self.ws.cell(row=row, column=colunm).value = cellvalue
self.wb.save(self.new_file)
except:
self.ws.cell(row=row, column=colunm).value = "writefail"
self.wb.save(self.new_file)
将第7列2-6行的数据执行结果输入至第10列的2-6行
for i in range(2, 7): cellvalue = work_sheet.cell(row=i, column=7).value manager_login_data = json.loads(cellvalue) res = requests.post(url=manager_login_url, data=manager_login_data) # 将每一次的响应结果写入到第10列 work_sheet.cell(row=i, column=10).value = res.json()['msg']