当我想使用 python reportlab 保存 pdf 时,我在使用非 ASCII 字符的路径时遇到了麻烦?

When I want to use the python reportlab to save a pdf ,I get the trouble when using path with nonASCII characters?

我写了一些简单的函数来从 reportlab 生成 pdf,但是当路径是非 ASCII(例如中文)时就会出现问题 UnicodeDecodeError: 'utf8' codec can't decode byte 0xd0 in position 3: invalid continuation byte。当路径全部为 ASCII 时,一切都很好。

我的代码很简单,就像

from reportlab.lib.pagesizes import letter, A4, landscape  
from reportlab.platypus import SimpleDocTemplate, Image  
from reportlab.lib.units import inch  
from reportlab.pdfgen import canvas
from reportlab import rl_settings
import reportlab.lib.pagesizes

filename_pdf = path + path.split('/')[-2]+'.pdf'
c = canvas.Canvas(filename_pdf, pagesize=maxsize )
#some code
c.save()
filename_pdf = filename_pdf.decode('gbk','ignore')