2017-05-18 1 views
2

나는이 사이트에서 'tuomastik'에 의해 훌륭한 코드를 부여 받았고, 저를 위해 약간 변형되었습니다. 그러나 여러 시간 (하나의 PDF이지만 각 페이지가 신선한 페이지에서 시작됨)이 아닌 하나의 PDF로 인쇄되도록 편집하는 데 몇 시간을 보냈지 만 HTML은 내가 원하는만큼 좋지 않아서 붙어있다.python - jinja2 - 파이썬에서 모든 HTML을 하나의 PDF로 바꾸기 (복수가 아닌)

코드는 다음과 같습니다

HTML

<html> 


<head> 
    <style type="text/css"> 
     html, body { 
      width: 500px; 
      font-size: 12px; 
      background: #fff; 
      padding: 0px; 
     } 
     #my-custom-table { 
      width: 500px; 
      border: 0; 
      margin-top: 20px; 
     } 
     #my-custom-table td { 
      padding: 5px 0px 1px 5px; 
      text-align: left; 
     } 
    </style> 
</head> 
<body> 


<table cellspacing="0" border="0" style="width:500px; border:0; font-size: 14px;"> 
    <tr> 
     <td style="text-align:left;"> 
      <b><span>Title of the PDF report - Row {{ row_ix + 1 }}</span></b> 
     </td> 
     <td style="text-align:right;"> 
      <b><span>{{ date }}</span></b> 
     </td> 
    </tr> 
</table> 


<table cellspacing="0" border="0.1" id="my-custom-table"> 
    {% for variable_name, variable_value in df.iteritems() %} 
    {% if loop.index0 == 0 %} 
    <tr style="border-top: 1px solid black; 
       border-bottom: 1px solid black; 
       font-weight: bold;"> 
     <td>Variable name</td> 
     <td>Variable value</td> 
    </tr> 
    {% else %} 
    <tr> 
     <td>{{ variable_name }}</td> 
     <td>{{ variable_value }}</td> 
    </tr> 
    {% endif %} 
    {% endfor %} 
</table> 


</body> 
</html> 

파이썬

from datetime import date 

import jinja2 
import pandas as pd 
from xhtml2pdf import pisa 

df = pd.read_csv('data.csv', encoding='cp1252') 

for row_ix, row in df.iterrows(): 

    html = jinja2.Environment( # Pandas DataFrame to HTML 
     loader=jinja2.FileSystemLoader(searchpath='')).get_template(
     'report_template.html').render(date=date.today().strftime('%d, %b %Y'), 
             row_ix=row_ix, df=row) 

    # Convert HTML to PDF 
    with open('report_row_%s.pdf' % (row_ix+1), "w+b") as out_pdf_file_handle: 
     pisa.CreatePDF(
      src=html, # HTML to convert 
      dest=out_pdf_file_handle) # File handle to receive result 

사람이 도움을 줄 수에게? 나는 파이썬 파일에서 for 루프를 제거해야하지만 html 파일을 어떻게 처리해야하는지에 대해서는 확신하지 못한다.

내가 싫어하지만이 좋은 시도를 줬어.하지만 내가 HTML에서 루프를 엉망으로 만들 때 실행할 수 없다.

많은 감사

답변

1

report_template.html

<html> 


<head> 
    <style type="text/css"> 
     html, body { 
      width: 500px; 
      font-size: 12px; 
      background: #fff; 
      padding: 0px; 
     } 
     #my-custom-table { 
      width: 500px; 
      border: 0; 
      margin-top: 20px; 
     } 
     #my-custom-table td { 
      padding: 5px 0px 1px 5px; 
      text-align: left; 
     } 
    </style> 
</head> 
<body> 


{% for row_ix, row in df.iterrows() %} 

    <table cellspacing="0" border="0" style="width:500px; border:0; font-size: 14px;"> 
     <tr> 
      <td style="text-align:left;"> 
       <b><span>Title of the PDF report - Row {{ row_ix + 1 }}</span></b> 
      </td> 
      <td style="text-align:right;"> 
       <b><span>{{ date }}</span></b> 
      </td> 
     </tr> 
    </table> 

    <table cellspacing="0" border="0.1" id="my-custom-table"> 
     {% for variable_name, variable_value in row.iteritems() %} 
     {% if loop.index0 == 0 %} 
     <tr style="border-top: 1px solid black; 
        border-bottom: 1px solid black; 
        font-weight: bold;"> 
      <td>Variable name</td> 
      <td>Variable value</td> 
     </tr> 
     {% else %} 
     <tr> 
      <td>{{ variable_name }}</td> 
      <td>{{ variable_value }}</td> 
     </tr> 
     {% endif %} 
     {% endfor %} 
    </table> 

    <!-- Page break (the syntax is for xhtml2pdf) --> 
    <pdf:nextpage /> 

{% endfor %} 


</body> 
</html> 

파이썬

from datetime import date 

import jinja2 
import pandas as pd 
from xhtml2pdf import pisa 

df = pd.DataFrame({ 
    "Average Introducer Score": [9, 9.1, 9.2], 
    "Reviewer Scores": ["Academic: 6, 6, 6", "Something", "Content"], 
    "Average Academic Score": [5.7, 5.8, 5.9], 
    "Average User Score": [1.2, 1.3, 1.4], 
    "Applied for (RC)": [9.2, 9.3, 9.4], 
    "Applied for (FEC)": [5.5, 5.6, 5.7], 
    "Duration (Months)": [36, 37, 38]}) 

html = jinja2.Environment( # Pandas DataFrame to HTML 
    loader=jinja2.FileSystemLoader(searchpath='')).get_template(
    'report_template.html').render(date=date.today().strftime('%d, %b %Y'), 
            df=df) 

# Convert HTML to PDF 
with open('report.pdf', "w+b") as out_pdf_file_handle: 
    pisa.CreatePDF(
     src=html, # HTML to convert 
     dest=out_pdf_file_handle) # File handle to receive result 
+0

지금까지 정말 감사합니다! ....이 위대하고 완벽하게 작동합니다! – ScoutEU