Commit f229262f authored by wang zhengwei's avatar wang zhengwei

添加jinjia2html.py html_to_pdf.py

parent 61072755
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name : html_to_pdf.py
# @Time : 2020/11/19 上午11:07
# @Author : X. Peng
# @Email : acepengxiong@163.com
# @Software : PyCharm
# -----------------------------------------------------------------------------
import pdfkit
from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter
def html_to_pdf():
options = {
'--enable-local-file-access': '--enable-local-file-access',
# 'encoding': "utf-8",
# 'page-size': 'A5',
# 'page-width': '300mm',
# 'page-height': '200mm',
# 'margin-top': '0mm',
# 'margin-right': '0mm',
# 'margin-bottom': '0mm',
# 'margin-left': '0mm'
}
url1 = 'https://www.jianshu.com'
# url = 'http://baidu.com'
url2 = 'https://manage.meerkat.top'
# pdfkit.from_url(url1, '../pdf/out5.pdf', options=options)
# pdfkit.from_file('../templates/monthReport.html', '../pdf/out5.pdf', options=options)
pdfkit.from_file('app/html/monthReport.html', 'app/pdf/out5.pdf', options=options)
def merge_pdf(pdfFiles, target_file='/Users/pengxiong/Desktop/combine.pdf'):
""""""
merger = PdfFileMerger()
pdfWriter = PdfFileWriter() # 生成一个空白的pdf文件
for fileName in pdfFiles:
pdfReader = PdfFileReader(open(fileName, 'rb')) # 以只读方式依次打开pdf文件
for pageNum in range(pdfReader.numPages):
print(pdfReader.getPage(pageNum))
pdfWriter.addPage(pdfReader.getPage(pageNum)) # 将打开的pdf文件内容一页一页的复制到新建的空白pdf里
pdfOutput = open(target_file, 'wb') # 生成combine.pdf文件
pdfWriter.write(pdfOutput) # 将复制的内容全部写入combine.pdf
if __name__ == '__main__':
html_to_pdf()
# merge_pdf(['/Users/pengxiong/Desktop/out1.pdf', '/Users/pengxiong/Desktop/out.pdf'])
from jinja2 import PackageLoader, Environment
from app.service.result_service import UserCustomerResultAdaptor
# 准备数据
ifa_id = 'USER_INFO15914346866762'
customer_id = '202009281545001'
user_customer = UserCustomerResultAdaptor(ifa_id, customer_id)
df = user_customer.calculate_total_data()
d = user_customer.calculate_group_result_data()
print(d)
# 几月综述部分
total_cost = df["total_cost"]/10000
now_yield = round((df['contribution_decomposition']-1)*100,2)
now_withdrawal = round(df["max_drawdown"][0]*100,2)
monthly_return_performance_pic = '_'.join((ifa_id, customer_id, '20201109', 'monthly_return_performance.png'))
image_dir = './img/'
monthly_return_performance_pic = image_dir + monthly_return_performance_pic
# 全部数据
data = {'customer_name':'成龙', 'customer_gender':'女',
'year_month':'2020年10月', 'ifa_company':'飞度工作室',
'title':'10月综述', 'brand_name':'飞度工作室',
'customer_old':42, 'customer_level':'平衡型',
'position_years':'5年', 'planned_allocation_amount':2000.00,
'now_allocation_amount':total_cost, 'now_yield':now_yield, 'expected_yield':20,
'now_withdrawal':now_withdrawal, 'expected_withdrawal':20,
'now_year_income':172623,'now_month_income':94477,'totoal_rate_of_return':19.99,
'month_rise':19.99, 'year_totoal_rate_of_return':19.32,
'annualised_return': 56.19, 'cost_of_investment':1000000,
'final_balance':2000400, 'total_profit':172623,
'latest_worth_day':'2020-11-05',
'index_comparison':{'section_return':5.75, 'annualized_returns': 0.40,
'volatility':2.29, 'max_withdrawal':2.15, 'sharpe_ratio':3.05},
'index_comparison_500':{'section_return':5.75, 'annualized_returns': 0.40,
'volatility':2.29, 'max_withdrawal':2.15, 'sharpe_ratio':3.05},
'monthly_return_performance_pic':monthly_return_performance_pic
}
# 开始渲染html模板
env = Environment(loader=PackageLoader('app','templates')) # 创建一个包加载器对象
template = env.get_template('monthReport.html') # 获取一个模板文件
monthReport_html = template.render(data) # 渲染
# 保存 monthReport_html
save_file = "app/html/monthReport.html"
with open(save_file, 'w', encoding="utf-8") as f:
f.write(monthReport_html)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment