将多个Blob转换为pdf

时间:2019-03-28 19:45:04

标签: python file pdf blob cx-oracle

我有多个Blob块,全部为28K字节大小,但最后一个可以相同或更小。 x.pdf有94个大块。该代码循环遍历94个块并结束而没有错误。任何人都使用多个Blob来创建单个文件。

已经使用PL / SQL创建了275K文件,现在大约停留在4K,对于UTL_FILE函数来说似乎太大了。

con = cx_Oracle.connect('sysadm/password@mydb')

cur = con.cursor() 
sql = 'select count(*) from chunk_record where filename = :sfn'
cur.execute(sql, sfn = 'x.pdf')
z = cur.fetchone()[0]
y = 0
with codecs.open('x.pdf', encoding='utf-8', mode='wb+') as file:
    bcur = con.cursor()
    for y in range (z):
        print(y)
        bsql = 'select file_data from chunk_record where filename = :sfn and file_seq = :seq'
        bcur.execute(bsql, sfn = 'x.pdf', seq = y)
        if type(bcur.fetchone()[0]) is cx_Oracle.BLOB:
             file.write(bcur.fetchone()[0].read())
    bcur.close()
file.close()
cur.close()
con.close()

以下python代码正在生成大小为零的x.pdf。当我尝试以pdf格式打开时,出现错误。大小应在28K * 93〜28K * 94之间

2 个答案:

答案 0 :(得分:0)

条件type(row[0]) is cx_Oracle.BLOB始终为false。由于cx_Oracle.BLOB是出现在结果中的列类型 设置了描述,但是类是cx_Oracle.LOB,所以要进行检查:

row = bcur.fetchone()
if isinstance(row[0], cx_Oracle.LOB):
    file.write(row[0].read())

文件最终为空的原因是它从未被写入。

其余答案都是正确的。


为什么要打开带有编码的文件?它应该是不应转码的二进制数据。

替换with codecs.open('x.pdf', encoding='utf-8', mode='wb+') as file:

使用with open('x.pdf', mode='wb+') as file:

答案 1 :(得分:0)


with open('x.pdf', mode='a') as file:

将wb +更改为已解决的问题,谢谢。