Google Colab上WeasyPrint的日语(非ascii)字体

时间:2019-03-03 00:23:54

标签: google-colaboratory weasyprint

我有这个用Python编写的模板,该模板最后导出png,但是日语字符无法正确显示。

我已经搜索并发现日语字体必须按照here的说明在本地下载和使用,因此我从Google下载了该字体并将其放在相同的目录中,但是没有运气。

我使用的是Google合作实验室,因此可能还会引起麻烦吗?

例如:

# A custom comparer class that compares two DataRow instances by their
# .FindName1 column.
class CustomTableComparer : Collections.Generic.IEqualityComparer[Data.DataRow] {
  [bool] Equals([Data.DataRow] $x, [Data.DataRow] $y) {
    return [string]::Equals($x.FindName1, $y.FindName1, 'Ordinal')
  }
  [int] GetHashCode([Data.DataRow] $row) {
    # Note: Any two rows for which Equals() returns $true must return the same
    #       hash code. Because *ordinal, case-sensitive* string comparison is
    #       used above, it's sufficient to simply call .GetHashCode() on
    #       the .FindName1 property value, but that would have to be tweaked
    #       for other types of string comparisons.
    return $row.FindName1.GetHashCode();
  }
}


# Use reflection to get a reference to a .Cast() method instantiation 
# that casts to IEnumerable<DataRow>.
$toIEnumerable = [Linq.Enumerable].GetMethod('Cast').MakeGenericMethod([Data.DataRow])

# Call .Except() with the casts and the custom comparer.
# Note the need to wrap the .Rows value in an aux. single-element
# array - (, ...) - for it to be treated as a single argument.
[Linq.Enumerable]::Except(
    $toIEnumerable.Invoke($null, (, $Table1.Rows)), 
    $toIEnumerable.Invoke($null, (, $Table2.Rows)), 
    [CustomTableComparer]::new()
)

导出的png显示如下: test.png

import pandas as pd from weasyprint import HTML, CSS from weasyprint.fonts import FontConfiguration from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('.')) template = env.get_template("template.html") name_var = "にほんご" template_vars = {"name" : name_var,} html_out = template.render(template_vars) font_config = FontConfiguration() css = CSS(string=''' @font-face { font-family: 'Noto Sans JP'; src: url('NotoSansJP-Regular.otf'); } @page { font-family: 'Noto Sans JP'; background-color: white; } ''', font_config=font_config) HTML(string=html_out).write_png("test.png",stylesheets=[css],font_config=font_config) 最后看起来像这样:

html_out

我在做什么错?感谢您的帮助。

更新

我发现Google Colab方面存在问题。但是我不知道Colab环境如何工作。有提示吗?

0 个答案:

没有答案