update 报告生成格式调整
This commit is contained in:
parent
f45902f99e
commit
d70cbdfeb0
|
@ -7,9 +7,11 @@ from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate
|
|||
from reportlab.platypus.tableofcontents import TableOfContents
|
||||
from reportlab.pdfgen import canvas
|
||||
|
||||
from Report.scripts.PdfStyle import cover_space, cover_company_style, cover_report_style, cover_time_style, chapter_style, \
|
||||
from Report.scripts.PdfStyle import cover_space, cover_company_style, cover_report_style, cover_time_style, \
|
||||
chapter_style, \
|
||||
section_style, adjust_line_width, para_style_single, para_style_bold, table_name, table_unit, table_mark, \
|
||||
adjust_table_widths, adjust_table_style, Seashell4, toc_style_1, table_content_style, toc_style_2, darkGolden
|
||||
adjust_table_widths, adjust_table_style, Seashell4, toc_style_1, table_content_style, toc_style_2, darkGolden, \
|
||||
adjust_table_data
|
||||
from Report.scripts.path_tool import gen_pdf_path, get_pic_path
|
||||
|
||||
# 页码起始设置
|
||||
|
@ -158,6 +160,6 @@ class ReportGenerator:
|
|||
|
||||
# 表格
|
||||
elif list(part.keys())[0] == '表格':
|
||||
width = adjust_table_widths(part['表格'])
|
||||
style = adjust_table_style(part['表格'])
|
||||
self.story.append(Table(part['表格'], colWidths=width, style=style))
|
||||
td = adjust_table_data(part['表格'])
|
||||
self.story.append(Table(td, style=style))
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import datetime
|
||||
import json
|
||||
import time
|
||||
|
||||
from Report.PdfReport import ReportGenerator
|
||||
from Report.scripts.contents import *
|
||||
|
||||
|
||||
|
@ -55,4 +57,7 @@ if __name__ == '__main__':
|
|||
with open('../Report/static/ReportTemplates/信息技术业.json', 'r', encoding='utf-8') as f:
|
||||
doc = json.load(f)
|
||||
model = ReportModel(data, doc)
|
||||
model.report_values()
|
||||
text_model = model.report_values()
|
||||
report_file_name = '{}_{}.pdf'.format(data['企业名称'], int(time.time()))
|
||||
pdf_report = ReportGenerator(name=report_file_name, text_model=text_model)
|
||||
pdf_report.gen_report()
|
|
@ -2,8 +2,9 @@ from reportlab.lib.colors import HexColor
|
|||
from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_RIGHT
|
||||
from reportlab.pdfbase import pdfmetrics
|
||||
from reportlab.pdfbase.ttfonts import TTFont
|
||||
from reportlab.lib.styles import ParagraphStyle as PS
|
||||
from reportlab.platypus import TableStyle
|
||||
from reportlab.lib.styles import ParagraphStyle as PS, ParagraphStyle
|
||||
from reportlab.lib.styles import getSampleStyleSheet
|
||||
from reportlab.platypus import TableStyle, Paragraph
|
||||
from reportlab.lib import colors
|
||||
|
||||
import pandas as pd
|
||||
|
@ -11,7 +12,6 @@ import numpy as np
|
|||
|
||||
from Report.scripts.path_tool import get_font_path
|
||||
|
||||
|
||||
# rgb
|
||||
darkGolden = '#C8A063'
|
||||
lightGrey = '#F4F2EF'
|
||||
|
@ -23,8 +23,10 @@ pdfmetrics.registerFont(TTFont('pingbold', get_font_path(font='PingBold.ttf')))
|
|||
pdfmetrics.registerFont(TTFont('SimHei', get_font_path(font='simhei.ttf')))
|
||||
|
||||
# toc
|
||||
toc_style_1 = PS(name='TOCHeading1', fontName='pingbold', fontSize=16, leftIndent=20, firstLineIndent=-20, spaceBefore=10, leading=16)
|
||||
toc_style_2 = PS(name='TOCHeading2', fontName='pingbold', fontSize=12, leftIndent=40, firstLineIndent=-20, spaceBefore=5, leading=12)
|
||||
toc_style_1 = PS(name='TOCHeading1', fontName='pingbold', fontSize=16, leftIndent=20, firstLineIndent=-20,
|
||||
spaceBefore=10, leading=16)
|
||||
toc_style_2 = PS(name='TOCHeading2', fontName='pingbold', fontSize=12, leftIndent=40, firstLineIndent=-20,
|
||||
spaceBefore=5, leading=12)
|
||||
|
||||
# table of content
|
||||
table_content_style = PS(name="table_content_style", fontName="pingbold", fontSize=18, leading=40, alignment=TA_CENTER)
|
||||
|
@ -34,45 +36,83 @@ cover_space = PS(name='cover_space', fontSize=0, leading=75, alignment=TA_CENTER
|
|||
cover_company_style = PS(name="cover_company_style", fontName="pingbold", fontSize=30, leading=60, alignment=TA_CENTER)
|
||||
cover_report_style = PS(name="cover_report_style", fontName="pingbold", fontSize=18, leading=400, alignment=TA_CENTER)
|
||||
cover_fecr_style = PS(name="cover_fecr_style", fontName="pingbold", fontSize=18, leading=20, alignment=TA_CENTER)
|
||||
cover_time_style = PS(name="cover_time_style", fontName="pingbold", fontSize=16, leading=40, alignment=TA_CENTER, spaceBefore=20)
|
||||
cover_time_style = PS(name="cover_time_style", fontName="pingbold", fontSize=16, leading=40, alignment=TA_CENTER,
|
||||
spaceBefore=20)
|
||||
|
||||
# chapter & section
|
||||
chapter_style = PS(name="chapter_style", fontName="pingbold", fontSize=18, leading=40, alignment=TA_LEFT, spaceBefore=6)
|
||||
section_style = PS(name="section_style", fontName="pingbold", fontSize=12, leading=30, alignment=TA_LEFT, spaceBefore=6, textColor=HexColor(darkGolden))
|
||||
section_style = PS(name="section_style", fontName="pingbold", fontSize=12, leading=30, alignment=TA_LEFT, spaceBefore=6,
|
||||
textColor=HexColor(darkGolden))
|
||||
|
||||
# table
|
||||
table_name = PS(name="table_name", fontName="pingbold", fontSize=8, leading=16, alignment=TA_CENTER, spaceBefore=2, textColor=HexColor(darkGrey))
|
||||
table_name = PS(name="table_name", fontName="pingbold", fontSize=8, leading=16, alignment=TA_CENTER, spaceBefore=2,
|
||||
textColor=HexColor(darkGrey))
|
||||
table_unit = PS(name="table_unit", fontName="SimHei", fontSize=6, leading=8, alignment=TA_RIGHT, spaceBefore=2)
|
||||
table_mark = PS(name="table_mark", fontName="SimHei", fontSize=6, leading=16, alignment=TA_LEFT, spaceBefore=2)
|
||||
table_style = getSampleStyleSheet()
|
||||
table_style.add(ParagraphStyle(fontName='SimHei', name='Song', leading=16, fontSize=8, spaceBefore=2, alignment=TA_CENTER))
|
||||
|
||||
# para
|
||||
para_style_single = PS(name="para_style_single", fontName="SimHei", fontSize=8, leading=18, alignment=TA_LEFT, spaceBefore=6)
|
||||
para_style_normal = PS(name="para_style_normal", fontName="SimHei", fontSize=8, leading=18, alignment=TA_LEFT, spaceBefore=6, firstLineIndent=16)
|
||||
para_style_bold = PS(name="para_style_bold", fontName="pingbold", fontSize=10, leading=18, alignment=TA_LEFT, spaceBefore=6)
|
||||
para_style_single = PS(name="para_style_single", fontName="SimHei", fontSize=8, leading=18, alignment=TA_LEFT,
|
||||
spaceBefore=6)
|
||||
para_style_normal = PS(name="para_style_normal", fontName="SimHei", fontSize=8, leading=18, alignment=TA_LEFT,
|
||||
spaceBefore=6, firstLineIndent=16)
|
||||
para_style_bold = PS(name="para_style_bold", fontName="pingbold", fontSize=10, leading=18, alignment=TA_LEFT,
|
||||
spaceBefore=6)
|
||||
para_bold_style = PS(name="para_style", fontName="pingbold", fontSize=9, leading=18, alignment=TA_LEFT, spaceBefore=6)
|
||||
|
||||
|
||||
# table
|
||||
def adjust_table_widths(list_):
|
||||
"""
|
||||
计算表格每列宽度
|
||||
Parameters:
|
||||
list_: list 表格数据
|
||||
Returns:
|
||||
result: list 表格宽度列表
|
||||
"""
|
||||
# 表格默认宽度
|
||||
total_width = 410
|
||||
df = pd.DataFrame()
|
||||
|
||||
for list__ in list_:
|
||||
df = df.append(dict(zip(range(len(list__)), list__)), ignore_index=True)
|
||||
|
||||
# 将列表中的数据替换成数据的长度,并将0置为NaN
|
||||
df = df.apply(lambda x: x.str.len())
|
||||
df = df.replace(0, np.NaN)
|
||||
|
||||
# 计算每列数据长度平均值占总数的多少,并转为列表
|
||||
width_rate = (df.mean() / df.mean().sum()).values.tolist()
|
||||
# 列数
|
||||
cols_num = len(width_rate)
|
||||
|
||||
# 判断首列是否小于默认宽度
|
||||
if width_rate[0] < 0.33:
|
||||
# 小于默认宽度,用当前数据重新计算各列宽度
|
||||
widths = [width_rate[0] * total_width] + [((1 - width_rate[0]) / (cols_num - 1)) * total_width] * (cols_num - 1)
|
||||
else:
|
||||
# 大于默认宽度,则继续使用默认宽度继续各列宽度
|
||||
widths = [0.33 * total_width] + [(0.67 / (cols_num - 1)) * total_width] * (cols_num - 1)
|
||||
|
||||
return widths
|
||||
|
||||
|
||||
def adjust_table_data(list_):
|
||||
"""
|
||||
调整表格数据样式
|
||||
Parameters:
|
||||
list_: list 表格数据
|
||||
Returns:
|
||||
result: list 处理好的表格数据
|
||||
"""
|
||||
for item_ in list_:
|
||||
for index in range(len(item_)):
|
||||
item_[index] = Paragraph(item_[index], table_style['Song'])
|
||||
|
||||
return list_
|
||||
|
||||
|
||||
def adjust_table_style(list_):
|
||||
return TableStyle([
|
||||
# 边框
|
||||
|
@ -92,7 +132,6 @@ def adjust_table_style(list_):
|
|||
|
||||
|
||||
def adjust_line_width(str_, font_size):
|
||||
|
||||
mark_length = 400
|
||||
trans_length = mark_length / font_size - 1
|
||||
|
||||
|
@ -110,7 +149,9 @@ def adjust_line_width(str_, font_size):
|
|||
if u'\u4e00' <= str_[i] <= u'\u9fa5':
|
||||
s2 += str_[i]
|
||||
n = n + 1
|
||||
elif str_[i] in ['!', '?', '。', '"', '#', '$', '%', '&', ''', '(', ')', '*', '+', ',', '-', '-', '/', ':', ';', '<', '=', '>', '@', '[', '\', ']', '^', '_', '`', '{', '|', '}', '~', '、', '〃', '》', '《', '「', '」', '『', '』', '【', '】', '〔', '〕', '〖', '〗', '〝', '〞', '“', '”']:
|
||||
elif str_[i] in ['!', '?', '。', '"', '#', '$', '%', '&', ''', '(', ')', '*', '+', ',', '-', '-', '/', ':', ';',
|
||||
'<', '=', '>', '@', '[', '\', ']', '^', '_', '`', '{', '|', '}', '~', '、', '〃', '》', '《', '「',
|
||||
'」', '『', '』', '【', '】', '〔', '〕', '〖', '〗', '〝', '〞', '“', '”']:
|
||||
s2 += str_[i]
|
||||
n = n + 1
|
||||
else:
|
||||
|
|
Loading…
Reference in New Issue