简体   繁体   English

使用Reportlab Python将日期框架添加到PDF

[英]Adding dateframe to PDF using Reportlab python

I am trying to display my date frame in a pdf using Lab Report. 我正在尝试使用Lab Report将日期框架显示为pdf。 I have figured out so far what I want but I do not know how to add the index so this is missing. 到目前为止,我已经弄清楚了我想要什么,但是我不知道如何添加索引,因此丢失了。 My pdf looks like this: My data frame: 我的pdf如下所示:我的数据框:

            00-04  04-08  08-12  12-16  16-20  20-24
2017-12-09  17671  16443  14300  15317  16398  16398
2017-12-10  16632  17611  16486  10562  10562  13014
2017-12-11  11355      0      0      0   8569  11697

My code: 我的代码:

from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import *
from reportlab.lib import colors
import pandas as pd
import random

PATH_OUT = "C:\\"

elements = []
styles = getSampleStyleSheet()
doc = SimpleDocTemplate("Analysis_Lists.pdf", pagesize=A3, rightMargin=30,leftMargin=30, topMargin=30,bottomMargin=18)
elements.append(Paragraph("Wind Park Available Flex Neg", styles['Title']))

data = [[random.random() for i in range(1,4)] for j in range (1,8)]
df = resultado(lista,df_output,labels)
lista = [df.columns[:,].values.astype(str).tolist()] + df.values.tolist()

ts = [('ALIGN', (1,1), (-1,-1), 'CENTER'),
     ('LINEABOVE', (0,0), (-1,0), 1, colors.black),
     ('LINEBELOW', (0,0), (-1,0), 1, colors.black),
     ('FONT', (0,0), (-1,0), 'Times-Bold'),
     ('LINEBELOW', (0,-1), (-1,-1), 1, colors.black),
     ('BACKGROUND',(1,1),(-2,-2),colors.white),
     ('TEXTCOLOR',(0,0),(1,-1),colors.black)]

table = Table(lista, style=ts)
s = getSampleStyleSheet()
s = s["BodyText"]
s.wordWrap = 'CJK'
Explanation = [["The lost HT flex in the last week for 50 Hz SRL is ."],
["The lost HT flex in the last week for Tennet SRL is "],
["The lost HT flex in the last week for Amprion SRL is "],
]

data2 = [[Paragraph(cell, s) for cell in row] for row in Explanation]
e=Table(data2,spaceAfter=20)

elements.append(table)
elements.append(e)

I have added columns and values but not sure how to add an index and my index is DateTime stamp: 我已经添加了列和值,但是不确定如何添加索引,而我的索引是DateTime标记:

l = df.index[:,].strftime('%Y/%m/%d')
l = l.tolist()
l = [u'2017/12/09', u'2017/12/10', u'2017/12/11']

Can someone give me a hint how to continue with this process? 有人可以提示我如何继续此过程吗? I am not familiar with reportlab. 我不熟悉reportlab。 Any help or suggestion would be great I have read Report Lab user guide 任何帮助或建议都很棒,我已经阅读了报告实验室用户指南

As mentioned in the comments, I think the PDF problem is a "Red-Herring". 如评论中所述,我认为PDF问题是“红鲱鱼”。 I think the true issue is just getting your index into the columns. 我认为真正的问题只是将索引添加到列中。

df_2 = df.reset_index().rename(columns = {'index':'date'}) 
#.rename is just renaming 'index' into 'date'. you can put anything you want in there

and now pass df2 into lista instead of df . 现在将df2而不是df传递到lista中。

The rest of the code will work as expected. 其余代码将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM