为什么PyPDF2和reportlab在插入文本时会删除空格?

时间:2018-03-06 17:52:46

标签: python reportlab pypdf2

我正在尝试将格式化文本插入PDF的最后一页。我正在使用PyPDF2和reportlab库来执行此操作。我使用的是Python 2.7。

由于某种原因,文本插入时没有空格,并且插入每个字符的新行(不是每个CRLF)。我哪里出错或者有更好的方法吗?

感谢。

PYTHON CODE:

# Libs
from PyPDF2 import PdfFileWriter, PdfFileReader, PdfFileMerger;
from reportlab.pdfgen import canvas; # PDF Editor 1
from reportlab.lib.pagesizes import letter; # PDF Editor 2
from reportlab.lib.units import inch; # PDF Editor 3

uniOCRText = 'This is a test string.';

# Create a new PDF with Reportlab
packet = io.BytesIO();
can = canvas.Canvas(packet, pagesize=letter);

textobject = can.beginText();
textobject.setTextOrigin(inch, 2.5*inch);
textobject.setFont("Times-Roman", 10);
i = 0;
for line in uniOCRText:
    i = i + 1;
    print("i = " + str(i) + " - line = " + str(line));
    textobject.textLine(line); # Error here deletes spaces!!!
textobject.setFillGray(0.4);
can.drawText(textobject);
can.save();

# Move to the beginning of the StringIO buffer
packet.seek(0);
new_pdf = PdfFileReader(packet);

# Add watermark
output = PdfFileWriter();

page = new_pdf.getPage(0);
output.addPage(page);

tempFolder = "Temp/TempPDF.pdf";
outputStream = open(tempFolder, "wb");
output.write(outputStream);
outputStream.close();

# Create a Merger PDF
merger = PdfFileMerger();
merger.append(PdfFileReader(open(pdfFileFromLoc, 'rb')));
merger.append(PdfFileReader(open(tempFolder, 'rb')));
merger.write(pdfFileDestLoc);

1 个答案:

答案 0 :(得分:0)

>>> for line in 'hello':
...     print(line)
... 
h
e
l
l
o

你正在迭代字符。调用变量line不会使解释器迭代行。您必须splitlines()并迭代结果列表:

>>> for line in 'hello\nbye'.splitlines():
...     print(line)
... 
hello
bye