【问题标题】:Trouble merging pdf files with PyPDF2 module for Python无法将 pdf 文件与 Python 的 PyPDF2 模块合并
【发布时间】:2014-10-21 15:13:42
【问题描述】:

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

'unicode' object has no attribute 'write'. 

我的代码基于this post。有人能看出什么问题吗?

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)

【问题讨论】:

    标签: python file pdf


    【解决方案1】:

    我放弃了使用 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多