我試圖從Excel中可用的數據創建發票。目前,我已經加載CSV數據並轉換同樣爲大熊貓數據幀,並嘗試使用ReportLab的從下面的代碼放入PDF:使用reportlab生成發票
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
from reportlab.lib import colors
doc = SimpleDocTemplate("form_letter.pdf",pagesize=A4,
rightMargin=72,leftMargin=72,
topMargin=72,bottomMargin=18)
Story=[]
logo = "company_logo.jpg"
limitedDate = "03/05/2010"
formatted_time = time.ctime()
full_name = "Infosys"
address_parts = ["411 State St.", "Marshalltown, IA 50158"]
im = Image(logo, 2*inch, 1*inch,hAlign='LEFT')
Story.append(im)
Story.append(Spacer(1, 12))
styles=getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
styles.add(ParagraphStyle(name='RIGHT', alignment=TA_RIGHT))
ptext = '<font size=12>%s</font>' % formatted_time
Story.append(Paragraph(ptext, styles["Normal"]))
Story.append(Spacer(1, 12))
# Create return address
ptext = '<font size=12> Customer Name: </font>'
Story.append(Paragraph(ptext, styles["Normal"]))
ptext = '<font size=12>%s</font>' % full_name
Story.append(Paragraph(ptext, styles["Normal"]))
Story.append(Spacer(1,12))
for part in address_parts:
ptext = '<font size=12>%s</font>' % part.strip()
Story.append(Paragraph(ptext, styles["RIGHT"]))
Story.append(Spacer(1, 12))
ptext = '<font size=12>Dear %s:</font>' % full_name.split()[0].strip()
Story.append(Paragraph(ptext, styles["Normal"]))
Story.append(Spacer(1, 12))
ptext = '<font size=12>We would like to welcome you to our subscriber base
for Magazine! \
You will receive issues at the excellent introductory price of . Please
respond by\
to start receiving your subscription and get the following: .</font>'
Story.append(Paragraph(ptext, styles["Justify"]))
Story.append(Spacer(1, 12))
data = [[random.random() for i in range(1,4)] for j in range (1,8)]
df = pd.DataFrame (data)
lista = [df.columns[:,].values.astype(str).tolist()] + df.values.tolist()
t_style = TableStyle([('ALIGN',(1,1),(-2,-2),'RIGHT'),
('TEXTCOLOR',(1,1),(-2,-2),colors.red),
('VALIGN',(0,0),(0,-1),'TOP'),
('TEXTCOLOR',(0,0),(0,-1),colors.blue),
('ALIGN',(0,-1),(-1,-1),'CENTER'),
('VALIGN',(0,-1),(-1,-1),'MIDDLE'),
('TEXTCOLOR',(0,-1),(-1,-1),colors.green),
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0,0), (-1,-1), 0.25, colors.black),
])
s = getSampleStyleSheet()
s = s["BodyText"]
s.wordWrap = 'CJK'
t=Table(lista)
t.setStyle(t_style)
Story.append(t)
Story.append(Spacer(1, 12))
ptext = '<font size=12>Thank you very much and we look forward to serving you.</font>'
Story.append(Paragraph(ptext, styles["Justify"]))
Story.append(Spacer(1, 12))
ptext = '<font size=12>Sincerely,</font>'
Story.append(Paragraph(ptext, styles["Normal"]))
Story.append(Spacer(1, 48))
ptext = '<font size=12>Ima Sucker</font>'
Story.append(Paragraph(ptext, styles["Normal"]))
Story.append(Spacer(1, 12))
doc.build(Story)
我成功地創建PDF,但是,我無法執行以下操作:
當前客戶名稱顯示正常。我希望它是大膽的。我怎樣才能使它大膽?
地址在Customer Name後面。但是,我想要將地址打印在同一行上,但朝右側打印。我怎樣才能做到這一點?
在表格中,我希望我的頂部行和底部行變成粗體,頂部行應該填充一些顏色。
請讓我知道如何解決這3個問題。
Hi Gherta,謝謝你的回覆。你可以讓我知道這樣的圖書館,因爲我不知道任何。 –