一、案例1:insert_image
import xlsxwriter
book = xlsxwriter.Workbook('E:\\test.xlsx')
sheet = book.add_worksheet('demo')
sheet.insert_image('D4','E:\\001.jpg')
book.close()
二、案例2:
import xlsxwriter
import pymysql.cursors
connect = pymysql.Connect(
host='localhost',
port=3306,
user='root',
passwd='admin123',
db='enterprise',
charset='utf8'
)
try:
wb = xlsxwriter.Workbook('enterprise_6.xlsx')
ws = wb.add_worksheet('企业信息')
header = ['ID', '名称', '电话(图片)', '电话', '联系人', '邮箱', '网址', '地址']
for index, h in enumerate(header):
ws.write(0, index, h)
with connect.cursor() as cursor:
sql = 'select `id`,`name`,`phone_pic`,`phone`,`linkman`,`email`,`weburl`,`address` from `ep_company_info` where `id`<=12000 and `id`>10000'
data = cursor.execute(sql)
data_ = cursor.fetchall()
j = 1
for i, v in enumerate(data_):
ws.write(j, 0, v[0])
ws.write(j, 1, v[1])
if v[2] == '':
ws.write(j, 2, v[2])
else:
ws.insert_image(j, 2, './company/' + str(v[2]))
ws.write(j, 3, v[3])
ws.write(j, 4, v[4])
ws.write(j, 5, v[5])
# 网址
ws.write_url(j, 6, v[6])
ws.write(j, 7, v[7])
j += 1
finally:
wb.close()
print('完成')
import io
import xlsxwriter
from PIL import Image
im = Image.open('1.jpeg')
(x, y) = im.size # read image size
x = round(x / 1.14974) # 人为修正拉伸
y = round(y / 0.97516)
out = im.resize((x, y), Image.ANTIALIAS) # resize image with high-quality
image_data = io.BytesIO()
out.save(image_data, 'png')
book = xlsxwriter.Workbook('pict.xlsx')
sheet = book.add_worksheet('demo')
sheet.insert_image('D4', '1.jpeg',{'image_data': image_data})
book.close()
代码无问题:将代码插入到excel指定位置,并且可以设置图片尺寸
def btn_excl_method(self):
"""导出excel入口函数"""
wiz_obj = self.env['talent.export.export.data.wizard']
filename = '导出图片'
import xlsxwriter
from PIL import Image
output = io.BytesIO()
book = xlsxwriter.Workbook(output)
sheet = book.add_worksheet('TEST')
buf_image = io.BytesIO(base64.b64decode(self.image_1920))
sheet.write('A1', self.name)
sheet.insert_image('D1', "001.jpg", {'image_data': buf_image, 'x_scale': 0.5, 'y_scale': 0.5, 'positioning': 5})
book.close()
wiz_id = wiz_obj.sudo().create({
'file_data': base64.encodebytes(output.getvalue())
})
value = dict(
type='ir.actions.act_url',
target='self',
url='/web/content?model=%s&id=%s&field=file_data&download=true&filename=%s.xlsx' % (
'talent.export.export.data.wizard', wiz_id.id, filename),
)
return value
标签:PYTHON,image,excel,write,插入,book,ws,import,data
From: https://www.cnblogs.com/DTCLOUD/p/17138472.html