我正在尝试使用 python docx 将存储在数据库中的文本和图像获取到 word doc 中,文本检索得很好,在尝试检索图像时出现以下错误。AttributeError: 'bytes' 对象没有属性 'seek'。代码如下。
import sqlite3
from docx import Document
document = Document()
name = "The heading of the report"
document.add_heading(name,0)
connection = sqlite3.connect("demo.db")
cursor = connection.cursor()
cursor.execute("SELECT * FROM Users where UserID = 1")
images in binary format
data = cursor.fetchall()
for row in data:
zero = row[0]
one = row[1]
two = row[2]
document.add_paragraph(str(zero))
document.add_paragraph(str(one))
document.add_picture(two)
document.save('UserReport.docx')
connection.close()
便于测试的dB表结构如下:
CREATE TABLE Users (
UserID integer,
UserName text NOT NULL,
UserImage Blob,
PRIMARY KEY(`UserID`)
);
我可以看到由于这一行 document.add_picture(two) 而产生了错误。但不明白原因,不胜感激您的意见。