超强盘点!让Excel效率起飞的5个Python库
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/4e4c183bbce94cc4a5f73fe3dd3b8088~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728355704&x-signature=vUUEyOgg9W2K07tqlcARYTJTPsE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Excel<span style="color: black;">做为</span>Office的数据处理软件,<span style="color: black;">咱们</span>几乎<span style="color: black;">每日</span>都在<span style="color: black;">运用</span>。虽然好用,但在<span style="color: black;">海量</span>录入、处理数据的时候,效率未免有点低。<span style="color: black;">因此呢</span>,<span style="color: black;">非常多</span>学了Python的<span style="color: black;">朋友</span>,会利用Python的第三方库来批量操作Excel,<span style="color: black;">提高</span>效率。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python有<span style="color: black;">非常多</span>支持操作Excel的第三方库,今天<span style="color: black;">举荐</span>的5个库,<span style="color: black;">瞧瞧</span>它们是<span style="color: black;">怎样</span>让Excel效率起飞的!</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">Xlwings</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Xlwings是非常强大的处理Excel的库,无论是Windows还是Mac,Excel还是WPS,都<span style="color: black;">能够</span><span style="color: black;">运用</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">它功能非常齐全,能<span style="color: black;">非常</span>方便地新建、打开、修改、<span style="color: black;">保留</span>Excel,<span style="color: black;">能够</span>和matplotlib、numpy以及pandas无缝连接,支持读写numpy、pandas数据类型,将matplotlib可视化图表导入到excel中。<span style="color: black;">另一</span>,还<span style="color: black;">能够</span>调用Excel文件中VBA写好的程序,<span style="color: black;">亦</span><span style="color: black;">能够</span>让VBA调用Python写的程序。</p>import xlwings as xw #导入库
app = xw.App(visible=True,add_book=False)
wb = app.books.add() #打开Excel程序
wb = xw.Book(example.xlsx) #打开已有工作簿
wb.save(example.xlsx) #<span style="color: black;">保留</span>工作簿
wb.close() #退出工作簿(可省略)
app.quit() #退出Excel
sht = wb.sheets #引用工作表,括号内是<span style="color: black;">第1</span>个sheet名
rng = sht.range(a1)
#rng = sht #引用单元格,<span style="color: black;">第1</span>行的<span style="color: black;">第1</span>列即a1
rng = sht.range(a1:a5) #引用区域
sht.range(a1).value = Hello #单元格A1,写入字符串‘Hello’
sht.range(a1).value = #默认按行<span style="color: black;">插进</span>:A1:D4分别写入1,2,3,4
sht.range(a2).options(transpose=True).value = #按列<span style="color: black;">插进</span>
sht.range(a6).expand(table).value = [,,] #多行输入
print(sht.range(a1:d4).value) #读取A1:D4
rng = sht.range(a1).expand(table)
nrows = rng.rows.count
a = sht.range(fa1:a{nrows}).value #读取Excel<span style="color: black;">第1</span>列
ncols = rng.columns.count
fst_col = sht.value #读取Excel<span style="color: black;">第1</span>行
sht.range(A1).column #获取单元格列标
sht.range(A1).row #获取行标
sht.range(A1).column_width #获取列宽
sht.range(A1).row_height #获取行高
print(sht.range(A1).column ,sht.range(A1).row ,sht.range(A1).column_width ,sht.range(A1).row_height )
sht.range(A1).rows.autofit() #行高自适应
sht.range(A1).columns.autofit()#列宽自适应
sht.range(A1).color=(34,156,65) #给单元格A1上背景色
sht.range(A1).color #返回单元格颜色的RGB值
print(sht.range(A1).color)
sht.range(A1).color = None #清楚单元格颜色
print(sht.range(A1).color)
sht.range(A1).formula==SUM(B6:B7) #输入公式,相应单元格执行结果
sht.range(A1).formula_array #获取单元格公式
sht.range(A1).value=[,] #向指定单元格位置写入批量信息
sht.range(A1).expand().value #<span style="color: black;">运用</span>expand()<span style="color: black;">办法</span>读取表中批量数据
print(sht.range(A1).expand().value)
import numpy as np
np_data = np.array((1,2,3))
sht.range(F1).value = np_data #写入numpy array数据类型
import pandas as pd
df = pd.DataFrame([, ], columns=)
sht.range(A5).value = df #将pandas DataFrame数据类型写入excel
sht.range(A5).options(pd.DataFrame,expand=table).value #将数据读取,输出类型为DataFrame
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure()
plt.plot()
sht.pictures.add(fig, name=MyPlot, update=True) #将matplotlib图表写入到excel表格里<h1 style="color: black; text-align: left; margin-bottom: 10px;">xlrd</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">xlrd<span style="color: black;">重点</span>是读取Excel,支持xlsx和xls格式的excel表格,<span style="color: black;">能够</span>实现指定表单、指定行列、指定单元格的读取。</p>import xlrd #导入库
data = xlrd.open_workbook(filename) #文件名以及路径,<span style="color: black;">倘若</span>路径<span style="color: black;">或</span>文件名有中文给前面加一个r拜师原生字符
# 获取book中一个工作表
table = data.sheets() #<span style="color: black;">经过</span>索引<span style="color: black;">次序</span>获取
table = data.sheet_by_index(sheet_indx)) #<span style="color: black;">经过</span>索引<span style="color: black;">次序</span>获取
table = data.sheet_by_name(sheet_name) #<span style="color: black;">经过</span>名<span style="color: black;">叫作</span>获取
names = data.sheet_names() #返回book中所有工作表的名字
data.sheet_loaded(sheet_name or indx) # <span style="color: black;">检测</span>某个sheet<span style="color: black;">是不是</span>导入完毕
nrows = table.nrows #获取该sheet中的有效行数
table.row(rowx) #返回由该行中所有的单元格对象<span style="color: black;">构成</span>的列表
table.row_slice(rowx) #返回由该列中所有的单元格对象<span style="color: black;">构成</span>的列表
table.row_types(rowx, start_colx=0, end_colx=None) #返回由该行中所有单元格的数据类型<span style="color: black;">构成</span>的列表
table.row_values(rowx, start_colx=0, end_colx=None) #返回由该行中所有单元格的数据<span style="color: black;">构成</span>的列表
table.row_len(rowx) #返回该列的有效单元格长度
ncols = table.ncols #获取列表的有效列数
table.col(colx, start_rowx=0, end_rowx=None) #返回由该列中所有的单元格对象<span style="color: black;">构成</span>的列表
table.col_slice(colx, start_rowx=0, end_rowx=None) #返回由该列中所有的单元格对象<span style="color: black;">构成</span>的列表
table.col_types(colx, start_rowx=0, end_rowx=None) #返回由该列中所有单元格的数据类型<span style="color: black;">构成</span>的列表
table.col_values(colx, start_rowx=0, end_rowx=None) #返回由该列中所有单元格的数据<span style="color: black;">构成</span>的列表
table.cell(rowx,colx) #返回单元格对象
table.cell_type(rowx,colx) #返回单元格中的数据类型
table.cell_value(rowx,colx) #返回单元格中的数据<h1 style="color: black; text-align: left; margin-bottom: 10px;">xlwt</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">xlwt<span style="color: black;">重点</span>是写入Excel,<span style="color: black;">能够</span>实现指定表单、指定单元格的写入,但<span style="color: black;">保留</span>的格式只支持xls格式。</p>import xlwt #导入模块
workbook = xlwt.Workbook(encoding=utf-8) #创建workbook 对象
worksheet = workbook.add_sheet(sheet1) #创建工作表sheet
worksheet.write(0, 0, hello) #往表中写内容,<span style="color: black;">第1</span>各参数 行,第二个参数列,第三个参数内容
workbook.save(students.xls) #<span style="color: black;">保留</span>表为students.xls
# 为内容设置style
workbook = xlwt.Workbook(encoding=utf-8)
worksheet = workbook.add_sheet(sheet1)
# 设置字体样式
font = xlwt.Font()
font.name = Time New Roman # 字体
font.bold = True # 加粗
font.underline = True # 下划线
font.italic = True # 斜体
style = xlwt.XFStyle()
style.font = font # 创建style
worksheet.write(0, 1, world, style)
workbook.save(students.xls) # <span style="color: black;">按照</span>样式创建workbook
# 合并单元格
workbook = xlwt.Workbook(encoding=utf-8)
worksheet = workbook.add_sheet(sheet1)
# <span style="color: black;">经过</span>worksheet调用merge()创建合并单元格
# <span style="color: black;">第1</span>个和第二个参数单表行合并,第三个和第四个参数列合并,
# 合并第0列到第2列的单元格
worksheet.write_merge(0, 0, 0, 2, first merge)
# 合并第1行第2行<span style="color: black;">第1</span>列的单元格
worksheet.write_merge(0, 1, 0, 0, first merge)
workbook.save(students.xls)
# 设置单元格的对齐方式
workbook = xlwt.Workbook(encoding=utf-8)
worksheet = workbook.add_sheet(sheet1)
alignment = xlwt.Alignment()
alignment.horz = xlwt.Alignment.HORZ_CENTER # 水平居中
alignment.vert = xlwt.Alignment.VERT_CENTER # 垂直居中
style = xlwt.XFStyle()
style.alignment = alignment
worksheet.col(0).width = 6666 # 设置单元格宽度
worksheet.row(0).height_mismatch = True
worksheet.row(0).height = 1000 # 设置单元格的高度
worksheet.write(0, 0, hello world, style)
workbook.save(center.xls)
# 设置单元格边框
workbook = xlwt.Workbook(encoding=utf-8)
worksheet = workbook.add_sheet(sheet1)
border = xlwt.Borders()
# DASHED虚线
# NO_LINE<span style="color: black;">无</span>
# THIN实线
border.left = xlwt.Borders.THIN
border.right = xlwt.Borders.THIN
border.top = xlwt.Borders.THIN
border.bottom = xlwt.Borders.THIN
style = xlwt.XFStyle()
style.borders = border
worksheet.write(1, 1, love, style)
workbook.save(dashed.xls)
# 设置单元格背景色
workbook = xlwt.Workbook(encoding=utf-8)
worksheet = workbook.add_sheet(sheet1)
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 3
style = xlwt.XFStyle()
style.pattern = pattern
worksheet.write(1, 1, shit, style)
workbook.save(shit.xls)
# 设置字体颜色
workbook = xlwt.Workbook(encoding=utf-8)
worksheet = workbook.add_sheet(sheet1)
font = xlwt.Font()
# 设置字体为红色
font.colour_index=xlwt.Style.colour_map
style = xlwt.XFStyle()
style.font = font
worksheet.write(0, 1, world, style)
workbook.save(students.xls)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">XlsxWriter</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">XlsxWriter<span style="color: black;">能够</span>用来写文本、数字、公式并支持单元格格式化、<span style="color: black;">照片</span>、图表、文档配置、自动过滤等特性,<span style="color: black;">不外</span>缺点<span style="color: black;">亦</span>很<span style="color: black;">显著</span>,<span style="color: black;">不可</span>用来读取和修改Excel文件。</p>import xlsxwriter # 导入库
work_book = xlsxwriter.Workbook(my first.xlsx) # 创建一个excel文件,文件名为"my first.xlsx"
work_sheet1 = work_book.add_worksheet() # 添加shhet1
work_sheet2 = work_book.add_worksheet(my excel.xlsx) # 添加sheet名字为my excel.xlsx
work_sheet3 = work_book.add_worksheet() # 不加参数,默认添加sheet3
# write_number:写入数字
# write_blank:写入空格
# write_formula:写入公式
# write_datetime:写入时间格式
# write_boolean:写入<span style="color: black;">规律</span>数据
# write_url:写入链接<span style="color: black;">位置</span>
work_sheet2.write_string(0, 0, this is write string!)
work_sheet2.write_number(A2, 123456)
work_sheet2.write_blank(A3, None)
work_sheet2.write_number(B1, 12)
work_sheet2.write_number(B2, 24)
work_sheet2.write_number(B3, 35)
work_sheet2.write_formula(B7, =sum(b1:b5))
work_sheet2.write_datetime(0, 3, datetime.datetime.strptime(2019-04-18, %Y-%m-%d),
work_book.add_format({num_format: yyyy-mm-dd}))
work_sheet1.write_boolean(0, 0, True)
work_sheet1.write_url(A2, http://www.toutiao.com)<h1 style="color: black; text-align: left; margin-bottom: 10px;">openpyxl</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">openpyxl 是比较火的操作excel表格的Python库,只支持03版本之后的 xlsx。</p># 创建工作簿 Workbook
from openpyxl import Workbook
workbook = Workbook() # 创建一个工作簿对象
workbook.save(test.xlsx) # <span style="color: black;">保留</span>这个工作簿,命名为test
# 打开已有工作簿
from openpyxl import load_workbook
workbook = load_workbook(test.xlsx) # #打开当前路径下的test表格
# 创建表
# <span style="color: black;">办法</span>1:<span style="color: black;">插进</span>到最后(default)
ws1 = wb.create_sheet("Mysheet")
# <span style="color: black;">办法</span>2:<span style="color: black;">插进</span>到最<span style="color: black;">起始</span>的位置
ws2 = wb.create_sheet("Mysheet", 0)
# <span style="color: black;">选取</span>现有的表
from openpyxl import load_workbook
workbook = load_workbook(test.xlsx) # 打开当前路径下的test表格
sheet = workbook # <span style="color: black;">选取</span>名字为first_sheet的表格页
# 删除表
from openpyxl import load_workbook
workbook = load_workbook(test.xlsx) # 打开当前路径下的test表格
sheet = workbook # <span style="color: black;">选取</span>名字为first_sheet的表格页
workbook.remove(sheet) #删除这张表
# <span style="color: black;">拜访</span>单元格
# <span style="color: black;">办法</span>1
cell1 = sheet
# <span style="color: black;">办法</span>2
cell2 = sheet.cell(row=1,column=2)
cell1.value = 123456 # 设置单元格的值
sheet.merge_cells(A1:A2) #合并A1和A2单元格<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">今天就暂时先<span style="color: black;">举荐</span>到<span style="color: black;">这儿</span>,有需要的<span style="color: black;">朋友</span>赶紧<span style="color: black;">保藏</span>起来~~</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">往期精彩内容:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">干货!统计学7种数据分析<span style="color: black;">办法</span>,超级实用</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">Python入门,<span style="color: black;">必定</span>要吃透这69个内置函数</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">Python 200个标准库汇总</a></p>
你的见解独到,让我受益匪浅,非常感谢。 交流如星光璀璨,点亮思想夜空。
页:
[1]