我正在尝试根据Excel中提供的数据创建发票。目前,我已经以CSV格式加载数据并将其转换为pandas数据框,并尝试使用以下代码中的Reportlab输入pdf:
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,但是,我无法执行以下操作:
目前客户名称显示正常。我希望它是大胆的。我怎样才能使它变得大胆?
地址出现在客户名称后面。但是,我希望地址打印在同一条线上,但是朝向右边。我怎样才能实现这一目标?
在表格中,我希望我的Top和行和底行变为粗体,顶行应填充一些颜色。
请告诉我如何解决这3个问题。
答案 0 :(得分:1)
下午好的帕万
因此,为了填充发票模板,我个人会使用以下python代码
#FUNCTION DEFINITION
def saveInvoice(templateName, saveWithName, valuesDictionary):
import win32com.client as win32
word = win32.gencache.EnsureDispatch('Word.Application')
word.Visible = False
myTemplate = word.Documents.Open(templateName)
for key in valuesDictionary:
find = word.Selection.Find
find.Text = key
find.Replacement.Text = valuesDictionary[key]
find.Execute(Replace=2,Forward=True)
myTemplate.SaveAs(saveWithName, FileFormat=17)
myTemplate.Close(False)
word.Quit()
#MAIN PROGRAM
templateName = 'C:\\Users\\Grossman\\workspace\\Invoicetemplate\\invoiceTemplate.docx'
saveWithName= 'C:\\Users\\Grossman\\workspace\\Invoicetemplate\\Modified1.pdf'
valuesDictionary = {'{PlaceHolder1}' : 'value1', '{PlaceHolder2}' : 'value2', '{PlaceHolder3}' : 'value3'}
saveInvoice(templateName, saveWithName, valuesDictionary)
您必须使用20个键和值收集字典中的所有Excel行值,其中键将是发票占位符,字典值将是要在单词模板中替换的值。
答案 1 :(得分:0)
作为算法逻辑,我建议:
有许多python库和方法可以做到这一点,取决于你最适合自己的方式。祝你好运。