存储在Microsoft SQL中的Python blob PDF - 转换回PDF

时间:2018-03-26 12:26:06

标签: python sql sql-server pdf blob

我有一个PDF文档存储为Microsoft SQL数据库中的Blob。我正在尝试将blob转换回PDF以在内存中打开以进行分析,并且还可能将其保存到本地驱动器。我尝试使用“.read”保存其中一个文档,但它给了我一个错误:

  

ValueError:嵌入的空字节

这是我的代码/尝试:

connect = pyodbc.connect(
Driver = driver,
Server = server,
Database = database,
User = username,
Password = password)

test_query = "SELECT TOP 1 * FROM test.PDFs"

df_test = pd.read_sql(test_query, connect)

df_test_pdf = df_test['RawDocument'][0]

with open(df_test_pdf, "rb") as f:
   b = f.read

print(df_test_pdf)

1 个答案:

答案 0 :(得分:0)

我用这个解决了它

with open("Output.pdf", "wb") as output_file:
    cursor.execute("SELECT TOP 1 RawDocument FROM test.PDFs")
    ablob = cursor.fetchone()
    output_file.write(ablob[0])

在这里得到了类似问题的答案:

Writing blob from SQLite to file using Python