使用Python Cairo创建多个PDF

时间:2013-06-17 18:31:00

标签: python windows cairo

好吧,我多年来一直在这里寻求帮助,现在我终于有了一个未被涵盖的问题。我正在尝试从我的AS / 400打印队列中获取.txt文件(这里没有问题),并将每个“页面”输出为PDF(下一步是将它们合并到一个文件中)。我已经将格式化部分缩小了,我可以正确地创建单页文档而没有任何问题。

我已经采用了生成单个页面的代码,并将其扩展为处理包含多个页面的文件,当使用adobe writer“打印”时。下面的代码在一个特定的位置崩溃python(报告错误到窗口),我只是想找到澄清原因。我目前的代码非常草率,我主要是首先寻找功能,而且我计划重写大部分功能。

输入()和print()都在那里,所以我可以看到发生错误的地方

崩溃的部分:

for line in lineDict[x]:
    context.show_text(line)
    yPos += 14
    context.move_to(10, yPos)
    context.show_page()
    surface.finish()
    print(line) #Testing where crash occurs 

以下是完整的程序

import cairo
width, height = 792,612
myText=open("QSYSPRT120972.txt","r")
yPos = 10
lineList=[]
lineDict=dict()
x=0
surface = cairo.PDFSurface(outPDF+".pdf", width, height)
context = cairo.Context(surface)
context.set_source_rgb( 1, 1, 1)
context.rectangle(0, 0, width, height)
context.fill()
context.set_font_size(8)
context.select_font_face( "Courier")
context.move_to(10, yPos)
context.set_source_rgb(0, 0, 0)

lineList=[str(line.replace('\x00', '').encode("ascii", "ignore")).lstrip('b\'').replace('\\n','').rstrip("'").replace(' ','  ') for line in myText]
outPDF=lineList[0]
outPDF=outPDF[0:6]#Get the file name from the first line on each page
input("Press Enter to continue")
print(lineList[0])
while '' in lineList:
    lineList.remove('')
ll=';'.join(lineList)
#print(ll)
LoL=ll.split(outPDF)
LoL.remove('')
input("Press enter")
for pages in LoL:
    yPos = 10
    outPDF=lineList[0]
    outPDF=outPDF[0:6]
    LoL[x]=outPDF+lol[x]
    LoL[x].split(";;")
    outPDF=outPDF+str(x)# Not the best way to do this, but its okay for now
    tempLOL=LoL[x]
    tempLOL=tempLOL.split(";")
    while '' in tempLOL:
    tempLOL.remove('')
    print(tempLOL) #More testing
    lineDict[x]=tempLOL
    for line in lineDict[x]: #Crash happens here. Works fine if I just print each line to console
        context.show_text(line)
        yPos += 14
        context.move_to(10, yPos)
        context.show_page()
        surface.finish()
        print(line) #Testing where crash occurs 
    x+=1
    print("File: "+outPDF+".pdf Created!")
#print(lineDict)
input()

仅输出单页PDF的版本

import cairo
width, height = 792,612
myText=open("QSYSPRT120972.txt","r")
yPos = 10
lineList=[]

lineList=[str(line.replace('\x00', '').encode("ascii", "ignore")).lstrip('b\'').replace('\\n','').rstrip("'").replace(' ','  ') for line in myText]
outPDF=lineList[0]
outPDF=outPDF[0:6]
surface = cairo.PDFSurface( outPDF+".pdf", width, height)
context = cairo.Context( surface)
context.set_source_rgb( 1, 1, 1)
context.rectangle( 0, 0, width, height)
context.fill()
context.set_font_size(8)
context.select_font_face( "Arial")
context.move_to( 10, yPos)
context.set_source_rgb( 0, 0, 0)
while '' in lineList:
    lineList.remove('')

for line in lineList:
    context.show_text(line)
    yPos += 14
    context.move_to(10, yPos)
context.show_page()
surface.finish()
print("File: "+outPDF+".pdf Created!")

这是我发现的关于这个问题的唯一相关问题,但是从我正在做的所有print()中,我知道每次迭代都会将我的列表设置为正确的数据。

python: open file, feed line to list, process list data

FWIW我在Windows XP上。

感谢。

0 个答案:

没有答案
相关问题