python:xlwings 操作 Excel 加入图片

pip install xlwings ;

xlwings-0.28.5-cp37-cp37m-win_amd64.whl (1.6 MB)

摘要:Make Excel fly: Interact with Excel from Python and vice versa.

Requires: pywin32

编写 xlwings_test.py 如下

python 复制代码
# -*- coding: utf-8 -*-
""" xlwings 结合 Matplotlib 运用,能将图片加入 Excel文件中"""
import matplotlib.pyplot as plt
import xlwings as xw

fig = plt.figure()  # 指定画布
# plt.plot([1, 2, 3, 4, 5])
plt.plot([36,5,3,25,78])
plt.plot([9,10,31,45])
plt.plot([6,14,45,31])
plt.savefig(r'd:\python\test.png')

book = xw.Book(r'D:/python/test.xlsx')
sheet = book.sheets[0]
#help(sheet.pictures)
#add(self, image, link_to_file=False, save_with_document=True,
#    left=None, top=None, width=None, height=None, name=None, update=False,
#    scale=None, format=None, anchor=None, export_options=None)
sheet.pictures.add(fig, left=100, top=100, name='mypic', update=True)
book.save(r'd:\python\test.xlsx')
book.close()

运行 python xlwings_test.py


pip install openpyxl ;

openpyxl-3.1.2-py2.py3-none-any.whl (249 kB)

et_xmlfile-1.1.0-py3-none-any.whl (4.7 kB)

摘要:A Python library to read/write Excel 2010 xlsx/xlsm files

编写 openpyxl_test1.py 如下

python 复制代码
# -- coding: utf-8 --
""" 插入一个图片,需要先安装 Pillow (PIL)"""
from openpyxl import load_workbook
from openpyxl.drawing.image import Image

book = load_workbook("d:/python/test1.xlsx")
sheet = book.active
img = Image(r'd:\python\test.png')
nrows = sheet.max_row    # 取最大行数
#help(sheet.add_image)
# sheet.add_image(img, anchor=None)
sheet.add_image(img, "B%d" %(nrows+2))
#print("B%d" %(nrows+2))
book.save("d:/python/test1.xlsx")

运行 python openpyxl_test1.py

注意: anchor='A1' # cell 表格中位置,我选取了'B6'

相关推荐
青瓷程序设计4 分钟前
植物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
习习.y28 分钟前
关于python中的面向对象
开发语言·python
hmbbcsm36 分钟前
练习python题目小记(六)
开发语言·python
wow_DG1 小时前
【Python✨】VS Code 秒开 Python 类型检查:一招 mypy + settings.json 让你的 Bug 原地现形!
python·json·bug
allbs1 小时前
spring boot项目excel导出功能封装——3.图表导出
spring boot·后端·excel
Aspect of twilight1 小时前
LeetCode华为大模型岗刷题
python·leetcode·华为·力扣·算法题
空影星1 小时前
高效追踪电脑使用时间,Tockler助你优化时间管理
python·django·flask
LiLiYuan.2 小时前
【Lombok库常用注解】
java·开发语言·python
不去幼儿园2 小时前
【启发式算法】灰狼优化算法(Grey Wolf Optimizer, GWO)详细介绍(Python)
人工智能·python·算法·机器学习·启发式算法
二川bro2 小时前
数据可视化进阶:Python动态图表制作实战
开发语言·python·信息可视化