使用 Reportlab 添加数据框到 PDF python
Adding dateframe to PDF using Reportlab python
我正在尝试使用实验室报告以 pdf 格式显示我的日期框架。到目前为止我已经弄清楚我想要什么但我不知道如何添加索引所以它丢失了。我的 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
我的代码:
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)
我已经添加了列和值,但不确定如何添加索引,我的索引是日期时间戳:
l = df.index[:,].strftime('%Y/%m/%d')
l = l.tolist()
l = [u'2017/12/09', u'2017/12/10', u'2017/12/11']
有人可以提示我如何继续这个过程吗?我对报告实验室不熟悉。任何帮助或建议都将非常有用 我已阅读 Report Lab 用户指南
如评论中所述,我认为 PDF
问题是 "Red-Herring"。我认为真正的问题只是将索引放入列中。
df_2 = df.reset_index().rename(columns = {'index':'date'})
#.rename is just renaming 'index' into 'date'. you can put anything you want in there
现在将 df2
传递给 lista
而不是 df
。
其余代码将按预期工作。
我正在尝试使用实验室报告以 pdf 格式显示我的日期框架。到目前为止我已经弄清楚我想要什么但我不知道如何添加索引所以它丢失了。我的 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
我的代码:
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)
我已经添加了列和值,但不确定如何添加索引,我的索引是日期时间戳:
l = df.index[:,].strftime('%Y/%m/%d')
l = l.tolist()
l = [u'2017/12/09', u'2017/12/10', u'2017/12/11']
有人可以提示我如何继续这个过程吗?我对报告实验室不熟悉。任何帮助或建议都将非常有用 我已阅读 Report Lab 用户指南
如评论中所述,我认为 PDF
问题是 "Red-Herring"。我认为真正的问题只是将索引放入列中。
df_2 = df.reset_index().rename(columns = {'index':'date'})
#.rename is just renaming 'index' into 'date'. you can put anything you want in there
现在将 df2
传递给 lista
而不是 df
。
其余代码将按预期工作。