分享11个Python自动化操作Excel的方法

作者:Python不吃辣 时间:2022-11-18 08:10:39 

前言:

今天我教大家如何利用Python自动化操作Excel,包括:介绍操作Excel的工具包、安装方法及操作Excel具体方法。对于每天有大量重复性工作的同学来说,这款工具绝对是福利。

一、openpyxl是什么

openpyxl是一个Python库,用于读取/写入Excel xlsx / xlsm / xltx / xltm文件。它的诞生是因为缺少可从Python本地读取/写入Office Open XML格式的库。官方文档

二、openpyxl安装

使用pip安装openpyxl建议在不带系统软件包的Python virtualenv中执行此操作:

pip install openpyxl

支持流行的lxml库(如果已安装)。这在创建大文件时特别有用。

三、openpyxl操作指南

1、创建工作簿

from openpyxl import Workbook
wb = Workbook()
ws_00 = wb.active #默认不取名称
ws_00['A1']= 'Python学习与数据挖掘'
ws_01 = wb.create_sheet("new_sheet", 0) # 取一个new_sheet的名称
ws_01['A1']= 23
wb.save('/Users/***/Desktop/document.xlsx')

分享11个Python自动化操作Excel的方法

2、写工作簿

from openpyxl import Workbook
from openpyxl.utils import get_column_letter
wb = Workbook()
dest_filename = '/Users/****/Desktop/empty_book.xlsx'
ws1 = wb.active
ws1.title = "range names"
for row in range(1, 40):
    ws1.append(range(600))
ws2 = wb.create_sheet(title="Pi")
ws2['F5'] = 3.14
ws3 = wb.create_sheet(title="Data")
for row in range(10, 20):
    for col in range(27, 54):
        _ = ws3.cell(column=col, row=row, value="{0}".format(get_column_letter(col)))
wb.save(filename = dest_filename)

3、插入图片

from openpyxl import Workbook
from openpyxl.drawing.image import Image

wb = Workbook()
ws = wb.active
ws['A1'] = 'You should see three logos below'
img = Image('/Users/***/work/logo.png')
ws.add_image(img, 'A1')

wb.save('/Users/***/document01.xlsx')

4、删除行和列

删除列F:H

ws.delete_cols(6, 3)

5、将工作表转换为数据框

df = DataFrame(ws.values)

6、2D区域图

from openpyxl import Workbook
from openpyxl.chart import (
    AreaChart,
    Reference,
    Series,
)
wb = Workbook()
ws = wb.active
rows = [
    ['Number', 'Batch 1', 'Batch 2'],
    [2, 40, 30],
    [3, 40, 25],
    [4, 50, 30],
    [5, 30, 10],
    [6, 25, 5],
    [7, 50, 10],
]
for row in rows:
    ws.append(row)
chart = AreaChart()
chart.title = "Area Chart"
chart.style = 13
chart.x_axis.title = 'Test'
chart.y_axis.title = 'Percentage'
cats = Reference(ws, min_col=1, min_row=1, max_row=7)
data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=7)
chart.add_data(data, titles_from_data=True)
chart.set_categories(cats)
ws.add_chart(chart, "A10")
wb.save("area.xlsx")

分享11个Python自动化操作Excel的方法

7、雷达图

from openpyxl import Workbook
from openpyxl.chart import (
    RadarChart,
    Reference,
)
wb = Workbook()
ws = wb.active
rows = [
    ['Month', "Bulbs", "Seeds", "Flowers", "Trees & shrubs"],
    ['Jan', 0, 2500, 500, 0,],
    ['Feb', 0, 5500, 750, 1500],
    ['Mar', 0, 9000, 1500, 2500],
    ['Apr', 0, 6500, 2000, 4000],
    ['May', 0, 3500, 5500, 3500],
    ['Jun', 0, 0, 7500, 1500],
    ['Jul', 0, 0, 8500, 800],
    ['Aug', 1500, 0, 7000, 550],
    ['Sep', 5000, 0, 3500, 2500],
    ['Oct', 8500, 0, 2500, 6000],
    ['Nov', 3500, 0, 500, 5500],
    ['Dec', 500, 0, 100, 3000 ],
]
for row in rows:
    ws.append(row)
chart = RadarChart()
chart.type = "filled"
labels = Reference(ws, min_col=1, min_row=2, max_row=13)
data = Reference(ws, min_col=2, max_col=5, min_row=1, max_row=13)
chart.add_data(data, titles_from_data=True)
chart.set_categories(labels)
chart.style = 26
chart.title = "Garden Centre Sales"
chart.y_axis.delete = True
ws.add_chart(chart, "A17")
wb.save("radar.xlsx")

分享11个Python自动化操作Excel的方法

8、使用公式

业务中需要批量处理的操作,我们可以代码化。Python利用Excel的公式功能来处理数据,可以达到事半功倍的效果。

from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('/Users/***/work/document01.xlsx')
ws1=wb.active
ws1["F2"] = "=SUM(B2:E2)"   # 使用公式
# Save the file
wb.save('/Users/***/Desktop/document01.xlsx')

9、给单元格设定字体颜色

from openpyxl import Workbook
from openpyxl.styles import colors
from openpyxl.styles import Font
wb = Workbook()
ws = wb.active
a1 = ws['A1']
d4 = ws['D4']
ft = Font(color=colors.RED)  # color="FFBB00",颜色编码也可以设定颜色
a1.font = ft
d4.font = ft
# If you want to change the color of a Font, you need to reassign it::
#italic 倾斜字体
a1.font = Font(color=colors.RED, italic=True) # the change only affects A1
a1.value = "abc"
# Save the file
wb.save("/Users/***/Desktop/document01.xlsx")

10、设定字体和大小

from openpyxl import Workbook
from openpyxl.styles import colors
from openpyxl.styles import Font
wb = Workbook()
ws = wb.active
a1 = ws['A1']
d4 = ws['D4']
a1.value = "abc"
from openpyxl.styles import Font
from copy import copy
ft1 = Font(name=u'宋体', size=14)
ft2 = copy(ft1)   #复制字体对象
ft2.name = "Tahoma"

11、设定单元格的边框、字体、颜色、大小和边框背景色

from openpyxl import Workbook
from openpyxl.styles import Font
from openpyxl.styles import NamedStyle, Font, Border, Side,PatternFill
wb = Workbook()
ws = wb.active
highlight = NamedStyle(name="highlight")
highlight.font = Font(bold=True, size=20,color= "ff0100")
highlight.fill = PatternFill("solid", fgColor="DDDDDD")#背景填充
bd = Side(style='thick', color="000000")
highlight.border = Border(left=bd, top=bd, right=bd, bottom=bd)
print dir(ws["A1"])
ws["A1"].style =highlight
# Save the file
wb.save("/Users/***/Desktop/document01.xlsx")

来源:https://blog.csdn.net/m0_59235508/article/details/123070210

标签:Python,自动化,Excel
0
投稿

猜你喜欢

  • python+matplotlib实现动态绘制图片实例代码(交互式绘图)

    2022-06-16 15:43:08
  • CSS写法性能

    2009-05-28 19:09:00
  • 详解Python flask的前后端交互

    2023-03-19 06:41:05
  • Azkaban3.81.x部署过程及遇到的坑

    2022-10-24 21:38:22
  • Python PyInstaller库基本使用方法分析

    2022-03-19 16:29:46
  • 用Python制作一个可以聊天的皮卡丘版桌面宠物

    2021-12-05 10:44:41
  • keras 特征图可视化实例(中间层)

    2021-12-05 22:54:46
  • Python3使用SMTP发送带附件邮件

    2022-02-10 12:05:53
  • mysql 8.0.15 安装配置图文教程

    2024-01-26 02:22:41
  • python编程通过蒙特卡洛法计算定积分详解

    2022-05-31 10:45:01
  • python主线程捕获子线程的方法

    2022-06-07 21:25:46
  • PHP fprintf()函数用法讲解

    2023-06-01 20:09:20
  • Python使用穷举法求两个数的最大公约数问题

    2022-01-20 21:26:51
  • 详解Python 函数如何重载?

    2021-04-11 04:48:30
  • Python装饰器的应用场景代码总结

    2022-09-22 19:24:54
  • 你不知道的Vue技巧之--开发一个可以通过方法调用的组件(推荐)

    2024-04-30 08:46:07
  • 一张图告诉你计算机编程语言的发展历史

    2023-03-29 15:16:42
  • python发腾讯微博代码分享

    2022-05-27 04:45:00
  • 如何在Python里使用ChatGPT及ChatGPT是什么?注册方式?

    2022-12-01 22:43:12
  • 关于INDEX SERVER+ASP建立查询引擎的一点心得

    2008-03-02 15:56:00
  • asp之家 网络编程 m.aspxhome.com