How to import data from sqlite using python-docx?

一曲冷凌霜 提交于 2019-12-11 12:47:13

问题


Requirement:

I need text and image data from sqlite3 db to be populated into a word doc.

What I'm doing:

I'm using python-docx library, using this documentation to get started.

Db structure:

CREATE TABLE Users(UserID integer PRIMARY KEY, UserName text NOT NULL, UserImage Blob)

My code:

import sqlite3
from docx import Document

document = Document()
document.add_heading("Test Report from Sql",0) # ---> Document heading name
connection = sqlite3.connect("demo.db")  # ---> Connection to Db
cursor = connection.cursor()
rows = cursor.execute("SELECT UserName FROM Users where UserID = 1") # ---> Trying to get text values from db and adding
p = document.add_paragraph(rows)

connection.close()
document.save('Report1.docx')

Error Message: TypeError: 'in ' requires string as left operand, not tuple

来源:https://stackoverflow.com/questions/53254894/how-to-import-data-from-sqlite-using-python-docx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!