繁体   English   中英

无法将PyPDF2模块与pdf文件合并为Python

[英]Trouble merging pdf files with PyPDF2 module for Python

我从一个表中构建了一个字典,该字典为我提供了每个键的pdf文件路径列表。 如果一个键有多个值,我想将pdf文件合并在一起,并在输出文件名中使用该键。 我尝试写出merged_file时遇到属性错误:

'unicode' object has no attribute 'write'. 

我是根据这篇文章编写我的代码 有人可以看到什么地方出问题了吗?

import arcpy, os, PyPDF2, shutil
arcpy.env.overwriteOutput = True

gb_xls = r'P:\Records\GIS\__Databases__\MapIndex\_MapSets_Grantors_Verification_Q.xlsx'
gb_gdb_tbl = r'C:\temp\temp.gdb\_MapSets_Grantors_Verification_Q'
gb_tbl_sort = r'C:\temp\temp.gdb\_MapSets_Grantors_Verification_Q'
gb_fields = ['Actual_SheetLabel','GBSheetLabel','Image_Path_Filename']
gb_dict = {}
v_list = []

lastkey = -1
lastvalue = ""

rows = sorted(arcpy.da.SearchCursor(gb_gdb_tbl,gb_fields))
for row in rows:
    k = row[0]
    v = row[2]
    if k not in gb_dict:
        gb_dict[k] = v
    if k == lastkey:
            v = str(lastvalue) + ', ' + str(v)
            gb_dict[k] = v
    lastkey = k
    lastvalue = v

merged_file = PyPDF2.PdfFileMerger()

for k,v in gb_dict.items():
    new_file = os.path.join(r'D:\GrantorBoxes_Merged_Pdfs',k+'.pdf')
    if len(str(v).split(',')) > 1:
        for i in [v]:
            val =  i.split(',')[0]
            merged_file.append(PyPDF2.PdfFileReader(val, 'rb'))
        merged_file.write(new_file)
    else:
        shutil.copyfile(v,new_file)

更新:

我有一些用于使用PyPDF2合并PDF文件的不同代码,这些文件将合并文件而不会出现错误。 现在我的问题是它合并的文件比我预期的要多。 我想遍历字典,查找每个键具有多个值(pdf文件)的项目,然后将这些值合并到一个由键命名的文件中。 我的循环或缩进一定有问题,但我看不到它是什么。 这是更新的代码:

import arcpy, os, PyPDF2, shutil
arcpy.env.overwriteOutput = True

gb_xls = r'P:\Records\GIS\__Databases__\MapIndex\_MapSets_Grantors_Verification_Q.xlsx'
gb_gdb_tbl = r'C:\temp\temp.gdb\_MapSets_Grantors_Verification_Q'
gb_tbl_sort = r'C:\temp\temp.gdb\_MapSets_Grantors_Verification_Q'
gb_fields = ['Actual_SheetLabel','GBSheetLabel','Image_Path_Filename']
gb_dict = {}

lastkey = -1
lastvalue = ""

rows = sorted(arcpy.da.SearchCursor(gb_gdb_tbl,gb_fields))
for row in rows:
    k = row[0]
    v = row[2]
    if k not in gb_dict:
        gb_dict[k] = v
    if k == lastkey:
        v = str(lastvalue) + ',' + str(v)
        gb_dict[k] = v
    lastkey = k
    lastvalue = v

merger = PyPDF2.PdfFileMerger()

for k,v in gb_dict.items():
    v_list = v.split(',')
    if len(v_list) > 1:
        for i in v_list:
            print k,',',i
            input = open(i,'rb')
            merger.append(input)
        output = open(os.path.join(r'D:\GrantorBoxes_Merged_Pdfs',k+'.pdf'), "wb")
        merger.write(output)
        print output
    else:
        new_file = os.path.join(r'D:\GrantorBoxes_Merged_Pdfs',k+'.pdf')
        shutil.copyfile(str(v),new_file)

我使用PyPDF2报废了,只是使用了包含一些PDF文档功能的arcpy映射模块。 下面的工作代码:

import arcpy, os, shutil
arcpy.env.overwriteOutput = True

gb_xls = r'P:\Records\GIS\__Databases__\MapIndex\_MapSets_Grantors_Verification_Q.xlsx'
gb_gdb_tbl = r'C:\temp\temp.gdb\_MapSets_Grantors_Verification_Q'
gb_tbl_sort = r'C:\temp\temp.gdb\_MapSets_Grantors_Verification_Q'
gb_fields = ['Actual_SheetLabel','GBSheetLabel','Image_Path_Filename']
gb_dict = {}
lastkey = -1
lastvalue = ""

rows = sorted(arcpy.da.SearchCursor(gb_gdb_tbl,gb_fields))
for row in rows:
    k = row[0]
    v = row[2]
    if k not in gb_dict:
        gb_dict[k] = v
    if k == lastkey:
        v = str(lastvalue) + ',' + str(v)
        gb_dict[k] = v
    lastkey = k
    lastvalue = v

for k in gb_dict.keys():
    val = gb_dict.get(k)
    val_list = gb_dict.get(k).split(',')
    pdf_path = os.path.join(r'D:\GrantorBoxes_Merged_Pdfs',k + '.pdf')
    out_pdf_file = arcpy.mapping.PDFDocumentCreate(os.path.join(r'D:\GrantorBoxes_Merged_Pdfs',k + '.pdf'))
    if len(val_list) > 1:
        for v in val_list:
            print k,v
            out_pdf_file.appendPages(v)
        print out_pdf_file
        out_pdf_file.saveAndClose()
    else:
        shutil.copyfile(str(val),pdf_path)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM