简体   繁体   English

使用 xlsxwriter 在现有 Excel 工作表中插入图像

[英]Insert image in existing excel sheet by using xlsxwriter

i am trying to add image into the existing excel sheet by using xlsxwriter module我正在尝试使用 xlsxwriter 模块将图像添加到现有的 excel 表中

import xlsxwriter
workbook = xlsxwriter.Workbook('C:/Users/Desktop/blank.xlsx')
worksheet = workbook.get_worksheet_by_name('Sheet1') 
worksheet.insert_image('B5', 'C:/Users/Desktop/CaseDeatails/Abc.jpg')

i am getting the below error我收到以下错误

Traceback (most recent call last): File "C:\\Users\\Desktop\\insertImage.py", line 23, in worksheet.insert_image('B5', 'C:/Users/Desktop/CaseDeatails/Abc.jpg') AttributeError: 'NoneType' object has no attribute 'insert_image'回溯(最近一次调用):文件“C:\\Users\\Desktop\\insertImage.py”,第 23 行,在 worksheet.insert_image('B5', 'C:/Users/Desktop/CaseDeatails/Abc.jpg') AttributeError : 'NoneType' 对象没有属性 'insert_image'

Please help me on this error请帮我解决这个错误

inseart image in xlsxwriter xlsxwriter 中的插入图像

import xlsxwriter
import os
workbook = xlsxwriter.Workbook('C:/Users/Desktop/blank.xlsx')
worksheet = workbook.get_worksheet_by_name('Sheet1') 
image = os.path.join(settings.BASE_DIR, "C:/Users/Desktop/CaseDeatails/", "Abc.jpg")
worksheet.insert_image('B5', image)

That isn't possible with XlsxWriter since it cannot read or modify an existing file . XlsxWriter 无法做到这一点,因为它无法读取或修改现有文件

Try the OpenPyXL module instead.请尝试使用OpenPyXL模块。

Also for some reason python doesn't like the links to be:同样出于某种原因,python 不喜欢链接:

C:/Users/Desktop/blank.xlsx

They have to have a double / , so it should be:他们必须有一个双/ ,所以它应该是:

C://Users//Desktop//blank.xlsx

It is not a xlsxwriter solution, but it works well:它不是 xlsxwriter 解决方案,但效果很好:

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

wb = Workbook()
sheet1 = wb.create_sheet('sheet1',0)
active = wb['sheet1']
active.add_image(Image('fig.png'),'A1')

wb.save('myfile.xlsx')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM