xlwt、xlrd、xlutils可以完成读excel的读、写和修改操作,只需要通过pip安装相应的模块即可。
import xlrd
# 常规读取
filename = "test.xls" #文件路径
wb = xlrd.open_workbook(filename) # 加载这个xls文件
sh = wb.sheet_by_index(0) # 根据索引选择表格
sh.row(i) # 获取第i+1行的所有数据组成的list
sh.row_values(rowx, start_colx=0, end_colx=None) # 获取第rowx+1行的某几列的数据并组成list
rows = sh.nrows # 获取该表格所有行数
ncols = sh.ncols # 获取该表格所有列数
for i in range(1,rows): # 从第二行开始按行读取
title = sh.cell(i,0).value # 取第i+1行,第1列的数据
ctype = sh.cell(i,0).ctype # 取第i+1行,第1列的数据类型
# 根据列名获取某行的值
def get_col_index(name,workbook,sheet):
for i in range(sheet.ncols):
name2 = sheet.cell(0,i).value
print(name2)
if name2 == name:
return i
return -1
value = sh.cell(1,get_col_index("创建时间")).value
ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
from xlrd import xldate_as_tuple
cell = sh.cell(i,10)
if cell.ctype == 3:
date = xldate_as_tuple(sheet.cell(i,3).value,0)
pubtime = datetime.datetime(*date)
import xlwt
file = xlwt.Workbook() #新建个文件
sheet = file.add_sheet('test_sheet0',cell_overwrite_ok=True) #新建个表格,指定表名以及是否可覆盖写
sheet.write(i,j,value) #在第i+1行和第j+1列写value(i、j从0开始)
file.save("test_xls.xls") #保存成xls文件并指定文件名
# 设置某列的宽度
sheet.col(0).width = 200
# 写日期
style = xlwt.XFStyle()
style.num_format_str = 'D-MMM-YY'
worksheet.write(0, 0, datetime.datetime.now(), style)
# 写公式
sheet.write(1, 1, xlwt.Formula('SUM(A1,B1)'))
# 合并单元格的写
sheet.write_merge(0, 0, 0, 3, 'First Merge')
# write_merge(x, x + h, y, w + y, value, sytle) x和y表示当前行列,h和w为要合并的行数和列数
# 表格的style
# 修改字体:
style = xlwt.XFStyle()
font = xlwt.Font() # 为样式创建字体
font.name = 'Times New Roman' # 字体名称
font.bold = True # 黑体
font.colour_index = 2 # 修改字的颜色
font.underline = True # 下划线
style.font = font # 使用到样式中
sheet.write(0,1,"编号",style)
# 修改单元格背景色
style = xlwt.XFStyle()
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 5 #色号,可以在源代码中看到色号和颜色的对应关系
style.pattern = pattern
sheet.write(0,2,"编号",style)
思路:使用xlutils的copy函数复制一份xlrd读取到的workbook,并在此基础上进行写操作。
import xlrd
import pymysql
from xlrd import open_workbook
from xlutils.copy import copy
import xlwt
file_name = "test_update.xls"
workbook = xlrd.open_workbook(file_name)
sheet = wb.sheet_by_index(0)
workbook_update = copy(workbook) # 复制workbook
sheet_update = workbook_update.get_sheet(0)
sheet_update.write(i,j,value)
workbook_update.save(file_name)
然而,无论是xlrd、xlwt还是xlutils,对excel的操作都仅限excel一个表的大小小于等于65535行时才可以,大于65535行的话,就需要另外一个模块了,这就是openpyxl。
贴个demo,自己领悟- -
# # coding = utf-8
import openpyxl
from urllib.parse import urlparse
filename = "链接.xlsx"
workbook = openpyxl.load_workbook(filename)
worksheet = workbook.worksheets[0]
# worksheet = workbook.active
# 在第一列之前插入一列
#worksheet.insert_cols(15) #
print(worksheet.max_row)
print(worksheet.max_column)
for i in range(1,worksheet.max_row):
#print(row[2].value)
url = worksheet.cell(row=i, column=2).value
print(url)
if not url or "http" not in url:
continue
# print(url)
url = url[:url.find("?")] if "?" in url else url
domain_name = urlparse(url).netloc
print(domain_name)
worksheet.cell(row=i, column=3).value = domain_name
# # 枚举出来是tuple类型,从0开始计数
#
file_name = filename[:filename.find(".")] + "_结果.xlsx"
workbook.save(file_name)