tfse-model-api-v0.2/Report/scripts/PdfStyle.py

204 lines
8.2 KiB
Python
Raw Normal View History

2021-11-19 14:38:49 +08:00
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
2022-01-05 13:25:08 +08:00
from reportlab.lib.styles import ParagraphStyle as PS, ParagraphStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import TableStyle, Paragraph
2021-11-19 14:38:49 +08:00
from reportlab.lib import colors
import pandas as pd
import numpy as np
from Report.scripts.path_tool import get_font_path
2021-11-19 14:38:49 +08:00
# rgb
darkGolden = '#C8A063'
lightGrey = '#F4F2EF'
darkGrey = '#A1A8AB'
Seashell4 = '#8B8682'
# font
pdfmetrics.registerFont(TTFont('pingbold', get_font_path(font='PingBold.ttf')))
pdfmetrics.registerFont(TTFont('SimHei', get_font_path(font='simhei.ttf')))
2022-01-11 17:03:45 +08:00
pdfmetrics.registerFont(TTFont('SIMSUN', get_font_path(font='SIMSUN.ttf')))
2022-01-12 14:44:37 +08:00
pdfmetrics.registerFont(TTFont('SourceHanSerifCN-Bold', get_font_path(font='SourceHanSerifCN-Bold.ttf')))
2021-11-19 14:38:49 +08:00
# toc
2022-01-05 13:25:08 +08:00
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)
2021-11-19 14:38:49 +08:00
# table of content
table_content_style = PS(name="table_content_style", fontName="pingbold", fontSize=18, leading=40, alignment=TA_CENTER)
# cover
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)
2022-01-05 13:25:08 +08:00
cover_time_style = PS(name="cover_time_style", fontName="pingbold", fontSize=16, leading=40, alignment=TA_CENTER,
spaceBefore=20)
2021-11-19 14:38:49 +08:00
# chapter & section
chapter_style = PS(name="chapter_style", fontName="pingbold", fontSize=18, leading=40, alignment=TA_LEFT, spaceBefore=6)
2022-01-05 13:25:08 +08:00
section_style = PS(name="section_style", fontName="pingbold", fontSize=12, leading=30, alignment=TA_LEFT, spaceBefore=6,
textColor=HexColor(darkGolden))
2021-11-19 14:38:49 +08:00
# table
2022-01-05 13:25:08 +08:00
table_name = PS(name="table_name", fontName="pingbold", fontSize=8, leading=16, alignment=TA_CENTER, spaceBefore=2,
textColor=HexColor(darkGrey))
2021-11-19 14:38:49 +08:00
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)
2022-01-05 13:25:08 +08:00
table_style = getSampleStyleSheet()
2022-02-17 16:44:58 +08:00
table_style.add(
ParagraphStyle(fontName='SimHei', name='Song', leading=12, fontSize=8, spaceBefore=2, alignment=TA_CENTER))
table_style.add(
ParagraphStyle(fontName='SimHei', name='Long', leading=12, fontSize=8, spaceBefore=2, alignment=TA_LEFT))
table_style.add(
ParagraphStyle(fontName='SimHei', name='Longs', leading=9, fontSize=6, spaceBefore=2, alignment=TA_LEFT))
table_style.add(
ParagraphStyle(fontName='SimHei', name='Songs', leading=9, fontSize=6, spaceBefore=2, alignment=TA_CENTER))
table_style.add(
ParagraphStyle(fontName='SimHei', name='Song_small', leading=7, fontSize=5, spaceBefore=2, alignment=TA_CENTER))
2021-11-19 14:38:49 +08:00
# para
2022-01-05 13:25:08 +08:00
para_style_single = PS(name="para_style_single", fontName="SimHei", fontSize=8, leading=18, alignment=TA_LEFT,
spaceBefore=6)
2022-02-23 16:48:30 +08:00
para_style_special = PS(name="para_style_single", fontName="SimHei", fontSize=8, leading=18, alignment=TA_LEFT,
spaceBefore=6, textColor='red')
2022-01-05 13:25:08 +08:00
para_style_normal = PS(name="para_style_normal", fontName="SimHei", fontSize=8, leading=18, alignment=TA_LEFT,
spaceBefore=6, firstLineIndent=16)
2022-02-17 16:44:58 +08:00
para_style_esg = PS(name="para_style_esg", fontName="SimHei", fontSize=10, leading=18, alignment=TA_LEFT,
spaceBefore=6)
2022-01-05 13:25:08 +08:00
para_style_bold = PS(name="para_style_bold", fontName="pingbold", fontSize=10, leading=18, alignment=TA_LEFT,
spaceBefore=6)
2021-11-19 14:38:49 +08:00
para_bold_style = PS(name="para_style", fontName="pingbold", fontSize=9, leading=18, alignment=TA_LEFT, spaceBefore=6)
# table
def adjust_table_widths(list_):
2022-01-05 13:25:08 +08:00
"""
计算表格每列宽度
Parameters:
list_: list 表格数据
Returns:
result: list 表格宽度列表
"""
# 表格默认宽度
2021-11-19 14:38:49 +08:00
total_width = 410
df = pd.DataFrame()
for list__ in list_:
df = df.append(dict(zip(range(len(list__)), list__)), ignore_index=True)
2022-01-05 13:25:08 +08:00
# 将列表中的数据替换成数据的长度并将0置为NaN
2021-11-19 14:38:49 +08:00
df = df.apply(lambda x: x.str.len())
df = df.replace(0, np.NaN)
2022-01-05 13:25:08 +08:00
# 计算每列数据长度平均值占总数的多少,并转为列表
2021-11-19 14:38:49 +08:00
width_rate = (df.mean() / df.mean().sum()).values.tolist()
2022-01-05 13:25:08 +08:00
# 列数
2021-11-19 14:38:49 +08:00
cols_num = len(width_rate)
2022-01-05 13:25:08 +08:00
# 判断首列是否小于默认宽度
2021-11-19 14:38:49 +08:00
if width_rate[0] < 0.33:
2022-01-05 13:25:08 +08:00
# 小于默认宽度,用当前数据重新计算各列宽度
2021-11-19 14:38:49 +08:00
widths = [width_rate[0] * total_width] + [((1 - width_rate[0]) / (cols_num - 1)) * total_width] * (cols_num - 1)
else:
2022-01-05 13:25:08 +08:00
# 大于默认宽度,则继续使用默认宽度继续各列宽度
2021-11-19 14:38:49 +08:00
widths = [0.33 * total_width] + [(0.67 / (cols_num - 1)) * total_width] * (cols_num - 1)
return widths
2022-01-05 13:25:08 +08:00
def adjust_table_data(list_):
"""
调整表格数据样式
Parameters:
list_: list 表格数据
Returns:
result: list 处理好的表格数据
"""
for item_ in list_:
if len(item_) > 6:
for index in range(len(item_)):
if item_[index] != '报告期' and index == 0:
item_[index] = Paragraph(item_[index], table_style['Longs'])
else:
if index == 5 and len(item_[index]) > 10:
item_[index] = Paragraph(item_[index], table_style['Song_small'])
else:
item_[index] = Paragraph(item_[index], table_style['Songs'])
else:
for index in range(len(item_)):
if item_[index] != '报告期' and index == 0:
item_[index] = Paragraph(item_[index], table_style['Long'])
else:
item_[index] = Paragraph(item_[index], table_style['Song'])
2022-01-05 13:25:08 +08:00
return list_
2021-11-19 14:38:49 +08:00
def adjust_table_style(list_):
return TableStyle([
# 边框
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.grey),
('BOX', (0, 0), (-1, -1), 1, colors.black),
# 文字
('FONTNAME', (0, 0), (-1, -1), 'SimHei'),
('FONTSIZE', (0, 0), (-1, -1), 8),
('TEXTCOLOR', (0, 0), (-1, 0), colors.white),
# 排版
('ALIGN', (1, 0), (-1, -1), 'CENTER'),
# 背景
# ('BACKGROUND', (0, 0), (0, -1), HexColor(lightGrey)),
('BACKGROUND', (0, 0), (-1, 0), HexColor(darkGolden)),
])
def adjust_line_width(str_, font_size):
mark_length = 400
2022-01-05 13:25:08 +08:00
trans_length = mark_length / font_size - 1
2021-11-19 14:38:49 +08:00
if len(str_) < trans_length:
return str_
n = 0
s1 = ''
s2 = ''
for i in range(len(str_)):
if n == 0 and str_[i] in ['', '', '', '']:
continue
if u'\u4e00' <= str_[i] <= u'\u9fa5':
s2 += str_[i]
n = n + 1
2022-01-05 13:25:08 +08:00
elif str_[i] in ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '-', '', '', '',
'', '', '', '', '', '', '', '', '_', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '']:
2021-11-19 14:38:49 +08:00
s2 += str_[i]
n = n + 1
else:
s2 += str_[i]
n = n + 0.5
2022-01-05 13:25:08 +08:00
if (n > trans_length) and (i != len(str_) - 1):
if str_[i + 1] in ['', '', '', '']:
s2 += str_[i + 1]
2021-11-19 14:38:49 +08:00
s2 += '<br/>'
n = 0
s1 += s2
s2 = ''
else:
s2 += '<br/>'
n = 0
s1 += s2
s2 = ''
2022-01-05 13:25:08 +08:00
elif i == len(str_) - 1:
2021-11-19 14:38:49 +08:00
s1 += s2
return s1