Python 鸭嘴兽如何“猜测”粗体和斜体风格?
这是我的密码。我的第一个函数基于/Lib/site packages/reportlab/Lib/styles.py源代码,用于创建样式数组: 然后,我使用安装的字体构建自己的阵列:Python 鸭嘴兽如何“猜测”粗体和斜体风格?,python,reportlab,platypus,Python,Reportlab,Platypus,这是我的密码。我的第一个函数基于/Lib/site packages/reportlab/Lib/styles.py源代码,用于创建样式数组: 然后,我使用安装的字体构建自己的阵列: def render_to_response(self, context, **response_kwargs): # this is a response for Django, but the question is about styles response = HttpResponse(co
def render_to_response(self, context, **response_kwargs):
# this is a response for Django, but the question is about styles
response = HttpResponse(content_type='application/pdf; charset=utf-8')
# ! Hint : no filename -> the browser extracts it from the URL!
# -> create URLs like http://pdfreportlab.com/extract/myfilename.pdf
# this is the way to go to have 100% working UTF-8 filenames!
response['Content-Disposition'] = 'attachment; filename=""'
my_styles = self.create_styles([
('ms-regular', 'montserrat-regular',
'Montserrat-Regular'),
('ms-black', 'montserrat-black',
'Montserrat-Black'),
('ms-black-italic', 'montserrat-black-italic',
'Montserrat-BlackItalic'),
('ms-bold', 'montserrat-bold',
'Montserrat-Bold'),
('ms-bold-italic', 'montserrat-bold-italic',
'Montserrat-BoldItalic'),
])
doc = SimpleDocTemplate(response)
elements = []
c = canvas.Canvas(response, pagesize=A4, )
for idx in my_styles.byName:
p = Paragraph("Hello World <i>italic</i> <b>bold</b>",
style=my_styles[idx])
width, height = p.wrapOn(c, A4[0], A4[1])
elements.append(p)
doc.build(elements)
return response
除了和标签被忽略这一非常恼人的事实之外,一切都正常!它仅使用样式中的当前字体
你怎么能修改我的代码,使它考虑到标签,我最终得到文本本身带有标签的样式?如果你想让Platypus自动选择字体,你需要注册一个字体系列-你正在为每个字体变体创建不同的样式,所以当然和标签没有效果-它不知道使用什么字体因为传递的样式引用单个字体 以下是如何使用字体族构建样式:
from reportlab.lib.styles import ParagraphStyle
from reportlab.pdfbase.pdfmetrics import registerFont, registerFontFamily
from reportlab.pdfbase.ttfonts import TTFont
def create_paragraph_style(name, font_name, **kwargs):
ttf_path = "C:/Windows/Fonts/{}.ttf"
family_args = {} # store arguments for the font family creation
for font_type in ("normal", "bold", "italic", "boldItalic"): # recognized font variants
if font_type in kwargs: # if this type was passed...
font_variant = "{}-{}".format(font_name, font_type) # create font variant name
registerFont(TTFont(font_variant, ttf_path.format(kwargs[font_type])))
family_args[font_type] = font_variant # add it to font family arguments
registerFontFamily(font_name, **family_args) # register a font family
return ParagraphStyle(name=name, fontName=font_name, fontSize=10, leading=12)
然后,您可以将段落样式创建为:
pstyle = create_paragraph_style("ms", "montserrat",
normal="Montserrat-Regular",
bold="Montserrat-Bold",
boldItalic="Montserrat-BoldItalic")
当然,假设您的字体目录中有具有这些名称的TTF文件
然后,您可以将其添加到样式表中,特别是如果您想要父继承,请确保将其作为参数添加到ParagraphStyle创建中,或者直接将其用作:
p = Paragraph("Hello World <i>italic</i> <b>bold</b> <b><i>boldItalic</i></b>", style=pstyle)
独立斜体不会受到影响,因为我们没有在系列中定义它。正常、粗体、斜体、粗体:所有系统中都硬编码识别的字体变体?这是事实上的标准?@OlivierPons-这些是reportlab.pdfbase.pdfmetrics.registerFontFamily函数的“硬编码”参数签名是:registerFontFamilyfamily,normal=None,bold=None,italic=None,boldItalic=None-在上面的代码中,它是一种简写,因此您不必将名称从一个参数集转换/传递到另一个参数集。如果我在这里尝试这个硬编码的解决方案,那么它就可以工作了。我发现您的解决方案不起作用,因为我想这是一个bug。下面是我如何更改代码以使其正常工作的:1从循环中删除normal,2在循环前添加这一行:registerfonttfontfont_family,ttf_path.formatkwargs['normal']推断“normal”总是在kwargs中传递。3个小时。我发现,如果你在ttf文件中注册字体“Montserrat normal”,那么它就找不到Montserrat,你会得到以下错误:无法映射Montserrat的family/bold/italic。如果用同一个ttf文件注册“蒙特塞拉特”和“蒙特塞拉特普通”,则只能得到斜体文本!!。因此,解决办法是只注册“蒙特塞拉特”一次,而不注册“正常”一词。很确定这是个bug。PS:你的Python水平非常好!当我说我猜这是一个bug,它是关于鸭嘴兽和reportlab的,而不是你的代码,这是非常优秀的。每次我在reportlab中一步一步地调试时,我的眼睛都在灼热,我需要滴眼药水!
p = Paragraph("Hello World <i>italic</i> <b>bold</b> <b><i>boldItalic</i></b>", style=pstyle)