好的,基本上是关于我想要实现的目标,因为我多次被告知我所做的事情是无用的,或者我没有提供足够的信息,我会在这里尽力而为。我有一个程序可以在PDF文件上创建发票并打印出来。 python脚本来自所有必要客户/服务信息等的CSV文件。
现在,到目前为止,我的代码中的问题是我拥有的服务数量,以及在Python Reportlabs中,您必须选择在页面上绘制字符串的位置。一个命令示例是:
cursor.drawstring(xLocation,yLocation,String)
通常的做法是将字符串1到字符串6,在它上面的字符串下面逐一绘制。在接下来的三个示例中,第一个是如果所有服务都被收费,函数将如何输出。如果没有向客户收取某些服务,则第二个(中间)是输出。最后(RightMost)示例是代码我希望它输出。
Service 1 Service 1 Service 1
Service 2 Service 3
Service 3 Service 3 Service 4
Service 4 Service 4 Service 6
Service 5
Service 6 Service 6
不幸的是,基于布尔逻辑,以及有6种可能的服务这一事实,如果您愿意,每种都可以充电或不充电,空或满,那么有64种可能的方法来编写同样的功能来实现完美输出
如果我们为每个服务创建一个布尔值,0为空,1为满, 那么我们必须从000000-111111测试每一个可能性。这是64.编码是完全合理的。
但是,我需要的服务超过六项,所需的功能数量呈指数级增长。我没有编写每个函数,而是编写了一个程序,它将获取0和1集的文件,即:
0000000000
0000000001
0000000010
etc..
并测试每一行中的每一位。然后,此函数将根据行中的位为文件中的每一行写出一个函数。现在,它产生不正确的输出。我先告诉你我的剧本:
def testFIle():
# This Function will be used to enumerate through the same file you
used to create the switch dictionary,
# but will define a function for each line.
# filepath = '/home/smiley/Desktop/sampletenbools'
filepath = '/home/smiley/Desktop/10boolsALL'
with open(filepath) as fp:
num = 1
for cnt, line in enumerate(fp):
var = line
b1 = str(var)[0]
b2 = str(var)[1]
b3 = str(var)[2]
b4 = str(var)[3]
b5 = str(var)[4]
b6 = str(var)[5]
b7 = str(var)[6]
b8 = str(var)[7]
b9 = str(var)[8]
b10 = str(var)[9]
# YdrawLocationSVC1=490
# YdrawLocationSVC2=475
# YdrawLocationSVC3=460
# YdrawLocationSVC4=445
# YdrawLocationSVC5=430
# YdrawLocationSVC6=415
# YdrawLocationSVC7=400
# YdrawLocationSVC8=375
# YdrawLocationSVC9=360
# YdrawLocationSVC10=345
if b1 == "0":
YdrawLocationSVC1=1111
YdrawLocationSVC2=1
if b1 == "1":
YdrawLocationSVC1 = 1
if b2 == "0":
YdrawLocationSVC2=1111
YdrawLocationSVC3=2
if b2 == "1":
YdrawLocationSVC2 = 2
if b3 == "0":
YdrawLocationSVC3=1111
YdrawLocationSVC4=3
if b3 == "1":
YdrawLocationSVC3 = 3
if b4 == "0":
YdrawLocationSVC4=1111
YdrawLocationSVC5=4
if b4 == "1":
YdrawLocationSVC4 = 4
if b5 == "0":
YdrawLocationSVC5=1111
YdrawLocationSVC6=5
if b5 == "1":
YdrawLocationSVC5 = 5
if b6 == "0":
YdrawLocationSVC6=1111
YdrawLocationSVC7=6
if b6 == "1":
YdrawLocationSVC6 = 6
if b7 == "0":
YdrawLocationSVC7=1111
YdrawLocationSVC8=7
if b7 == "1":
YdrawLocationSVC7 = 7
if b8 == "0":
YdrawLocationSVC8=1111
YdrawLocationSVC9=8
if b8 == "1":
YdrawLocationSVC8 = 9
if b9 == "0":
YdrawLocationSVC9=1111
YdrawLocationSVC10=9
if b9 == "1":
YdrawLocationSVC9 = 9
if b10 == "0":
YdrawLocationSVC10=1111
if b10 == "1":
YdrawLocationSVC10 = 10
print "# Bitrep ="+str(var).strip("\n")
print "def Print"+str(num)+"():"
print"\tc.setFont('Deja', 12, leading=None)"
print "\t# SERVICE NAME"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC1)+", stringn1)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC2)+", stringn2)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC3)+", stringn3)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC4)+", stringn4)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC5)+", stringn5)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC6)+", stringn6)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC7)+", stringn7)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC8)+", stringn8)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC9)+", stringn9)"+\
"\n\tc.drawString(100, "+str(YdrawLocationSVC10)+", stringn10)"
# print("{} : {}".format(str(line).strip("\n"), "Print" + str(num)))
num += 1
# print "\n"
testFIle()
只要缺少一个服务,这个代码就会产生这样的正确输出:
# Bitrep =1111111101
def Print1022():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 3, stringn3)
c.drawString(100, 4, stringn4)
c.drawString(100, 5, stringn5)
c.drawString(100, 6, stringn6)
c.drawString(100, 7, stringn7)
c.drawString(100, 9, stringn8)
c.drawString(100, 1111, stringn9)
c.drawString(100, 10, stringn10)
# Bitrep =1111111110
def Print1023():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 3, stringn3)
c.drawString(100, 4, stringn4)
c.drawString(100, 5, stringn5)
c.drawString(100, 6, stringn6)
c.drawString(100, 7, stringn7)
c.drawString(100, 9, stringn8)
c.drawString(100, 9, stringn9)
c.drawString(100, 1111, stringn10)
# Bitrep =1111111111
def Print1024():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 3, stringn3)
c.drawString(100, 4, stringn4)
c.drawString(100, 5, stringn5)
c.drawString(100, 6, stringn6)
c.drawString(100, 7, stringn7)
c.drawString(100, 9, stringn8)
c.drawString(100, 9, stringn9)
c.drawString(100, 10, stringn10)
然而,我的代码中的逻辑打破了多个缺少的服务,如下所示:
# Bitrep =1111100101
def Print998():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 3, stringn3)
c.drawString(100, 4, stringn4)
c.drawString(100, 5, stringn5)
c.drawString(100, 1111, stringn6)
c.drawString(100, 1111, stringn7)
c.drawString(100, 9, stringn8)
c.drawString(100, 1111, stringn9)
c.drawString(100, 10, stringn10)
# Bitrep =1111100110
def Print999():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 3, stringn3)
c.drawString(100, 4, stringn4)
c.drawString(100, 5, stringn5)
c.drawString(100, 1111, stringn6)
c.drawString(100, 1111, stringn7)
c.drawString(100, 9, stringn8)
c.drawString(100, 9, stringn9)
c.drawString(100, 1111, stringn10)
如果有人可以解决这个问题,那么输出结果看起来就像文件中所有行的下一位一样,我将从你那里投票****:
def Print999():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 3, stringn3)
c.drawString(100, 4, stringn4)
c.drawString(100, 5, stringn5)
c.drawString(100, 1111, stringn6)
c.drawString(100, 1111, stringn7)
c.drawString(100, 6, stringn8)
c.drawString(100, 7, stringn9)
c.drawString(100, 1111, stringn10)
def Print764():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 1111, stringn3)
c.drawString(100, 3, stringn4)
c.drawString(100, 4, stringn5)
c.drawString(100, 1111, stringn6)
c.drawString(100, 1111, stringn7)
c.drawString(100, 5, stringn8)
c.drawString(100, 6, stringn9)
c.drawString(100, 1111, stringn10)
与往常一样,如果你到了我的帖子的末尾,我会留下深刻的印象lol。感谢任何为此付出最轻微努力的人,感谢所有帮助我做到这一点的人。
答案 0 :(得分:0)
不想让你的努力收缩,但人们一直在告诉你,你出错是因为某种原因:)
Reportlab允许您使用编程工具创建动态PDF文档,这意味着当内容更改时布局会发生变化。当你期待各种不同的内容时,试图对每一行的位置进行硬编码将很快变得笨拙/不可能,并且容易破解。
让自己轻松自如并使用循环代替。最简单的例子是,这里有一些基本页面列表的代码......
from reportlab.lib.units import cm
# get the data selection first
my_services_list = [1,3,6] # or my_dict, e.g. services {'1': 'message1', etc}
# set the cursor starting position
pos_X, pos_Y = 5, 15
for service in my_services_list:
c.drawString(pos_X*cm, pos_Y*cm, service)
# move the cursor down a line - remember that zero is the page bottom
pos_Y -= 1
就是这样,没有空行。 .drawString
适用于较简单的页面。如果您需要更全面的方法,请阅读有关框架和Flowables的文档。如果需要,您可以将文本流式传输到文本框中,就像使用桌面出版软件一样。