【发布时间】:2017-12-08 13:22:49
【问题描述】:
我正在尝试使用实验室报告在 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)
我添加了列和值,但不确定如何添加索引,我的索引是 DateTime stamp:
l = df.index[:,].strftime('%Y/%m/%d')
l = l.tolist()
l = [u'2017/12/09', u'2017/12/10', u'2017/12/11']
谁能给我一个提示如何继续这个过程?我不熟悉reportlab。我已阅读 Report Lab 用户指南
,任何帮助或建议都会很棒【问题讨论】:
-
我有点困惑。您是否已经拥有索引或者您正在尝试创建它?你想要的结果是什么?
-
@MattR 我只是缺少我的 pdf 表中的索引作为第一列。我将添加它现在的外观,用户看不到日期,例如由索引表示。
-
所以您希望数据框的索引改为列?
-
我想在 lista 中包含索引,以便用户可以看到日期@MattR
-
运行它时会发生什么?
df_2 = df.reset_index().rename(columns = {'index':'date'})现在将df2传递给lista而不是df
标签: python pandas pdf reportlab