0

我有以下代码尝试从 CSV 文件创建字典,然后将 CSV 中每个“行”的值附加到 PDF 中的新页面。我正在使用 pyPdf 和 ReportLab 来完成此任务。

源 PDF 包含 2 页,明信片的正面和背面,我试图为 CSV 文件中的每条记录复制它。例如,包含 25 条记录的 CSV 文件将生成包含 50 页的 PDF。(每张唱片一张正面一张背面)

我已经成功地创建了具有适当页数的 PDF,但是,我的代码部分旨在附加 CSV 文件中的值是为每一页附加相同的值,而不是每页的唯一值。

我很确定这与循环有关,因为打印字典会正确返回所有键和值对。我在这里做错了什么?

d = {}                        
csv_file = open(filename, 'rb')
reader = csv.reader(csv_file)
rownum = 0
for row in reader:
    total_rows += 1
    page_count = (total_rows - 1)
    if rownum == 0:
        header = row
    else:
        colnum = 0
        for col in row:
            d[header[colnum]] = col
            colnum += 1

        packet = StringIO.StringIO()

        can = canvas.Canvas(packet, pagesize=(621,405))
        can.drawString(340, 147, d['FirstName'])
        can.save()

        packet.seek(0)
        new_pdf = PdfFileReader(packet)                        

        existing_pdf = PdfFileReader(file(order, 'rb'))
        output = PdfFileWriter()

        front = existing_pdf.getPage(0)
        back = existing_pdf.getPage(1)
        back.mergePage(new_pdf.getPage(0))
        for i in range(0, page_count):
            output.addPage(front)
            output.addPage(back)

        outputStream = file(token+'_merged.pdf', 'wb')
        output.write(outputStream)
        outputStream.close()

    rownum += 1
4

1 回答 1

1

此循环为 CSV 中的每一行创建一个新文件,覆盖为早期行创建的版本。我对您正在使用的库并不完全熟悉,但它看起来像是一种将保存outputStream在主循环外部的方法可以解决它:

output = PdfFileWriter()
for rownum, row in enumerate(reader):
    if rownum == 0:
        header = row
    else:
        for col in row:
            d[header[colnum]] = col
            colnum += 1
        # or, more concisely:
        # d = dict((header[colnum], value) for (colnum, value) in enumerate(row))
        # or use a dict comprehension if you're on a sufficiently recent version
        # or best of all, use a csv.DictReader object instead of creating the dictionary yourself

        packet = StringIO.StringIO()

        can = canvas.Canvas(packet, pagesize=(621,405))
        can.drawString(340, 147, d['FirstName'])
        can.save()

        packet.seek(0)
        new_pdf = PdfFileReader(packet)                        

        # I would check whether this can be read once and still have getPage called multiple times
        existing_pdf = PdfFileReader(file(order, 'rb'))

        front = existing_pdf.getPage(0)
        back = existing_pdf.getPage(1)
        back.mergePage(new_pdf.getPage(0))
        output.addPage(front)
        output.addPage(back)

outputStream = file(token+'_merged.pdf', 'wb')
output.write(outputStream)
outputStream.close()
于 2013-09-27T17:43:35.390 回答