python
1# your code goes here# coding: utf-8 2import csv 3from mmap import PAGESIZE 4from reportlab.pdfgen import canvas 5from reportlab.lib.pagesizes import A4, portrait 6from reportlab.pdfbase import pdfmetrics 7from reportlab.pdfbase.ttfonts import TTFont 8 9# フォントの読み込み 10# ttfまたはttcファイルで指定して下さい 11pdfmetrics.registerFont(TTFont("IPAexg", "Fonts/ipaexg.ttf")) 12pdfmetrics.registerFont(TTFont("IPAexm", "Fonts/ipaexm.ttf")) 13file = "C:/Users/a/Desktop/output.pdf" 14page = canvas.Canvas(file,pagesize=portrait(A4)) 15# フォントの設定(第1引数:フォント、第2引数:サイズ) 16page.setFont("IPAexg", 24) 17 18 19#csvファイルの読み込み 20list_data = [] 21with open('list.csv', 'r', encoding="utf-8") as f: 22 csv_data = csv.reader(f) 23 list_data = [x for x in csv_data] 24 25height = 300 26for l in list_data: 27 # 指定座標が左端となるように文字を挿入 28 page.drawString(200, height, f"{l[0]}:{l[1]}") 29 height += 24 30 31# PDFファイルとして保存 32page.save() 33
Traceback (most recent call last): File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\lib\utils.py", line 537, in open_for_read return open_for_read_by_name(name,mode) File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\lib\utils.py", line 477, in open_for_read_by_name return open(name,mode) FileNotFoundError: [Errno 2] No such file or directory: 'Fonts/ipaexg.ttf' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\lib\utils.py", line 544, in open_for_read return BytesIO((datareader if name[:5].lower()=='data:' else rlUrlRead)(name)) File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\lib\utils.py", line 490, in rlUrlRead return urlopen(name).read() File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen return opener.open(url, data, timeout) File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 501, in open req = Request(fullurl, data) File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 320, in __init__ self.full_url = url File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 346, in full_url self._parse() File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 375, in _parse raise ValueError("unknown url type: %r" % self.full_url) ValueError: unknown url type: 'Fonts/ipaexg.ttf' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\pdfbase\ttfonts.py", line 151, in TTFOpenFile f = open_for_read(fn,'rb') File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\lib\utils.py", line 548, in open_for_read return open_for_read(name,mode) File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\lib\utils.py", line 546, in open_for_read raise IOError('Cannot open resource "%s"' % name) OSError: Cannot open resource "Fonts/ipaexg.ttf" During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\a\Desktop\ideone_S58vTq.py", line 11, in <module> pdfmetrics.registerFont(TTFont("IPAexg", "Fonts/ipaexg.ttf")) File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\pdfbase\ttfonts.py", line 1178, in __init__ self.face = TTFontFace(filename, validate=validate, subfontIndex=subfontIndex) File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\pdfbase\ttfonts.py", line 1072, in __init__ TTFontFile.__init__(self, filename, validate=validate, subfontIndex=subfontIndex) File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\pdfbase\ttfonts.py", line 439, in __init__ TTFontParser.__init__(self, file, validate=validate,subfontIndex=subfontIndex) File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\pdfbase\ttfonts.py", line 175, in __init__ self.readFile(file) File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\pdfbase\ttfonts.py", line 251, in readFile self.filename, f = TTFOpenFile(f) File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\reportlab\pdfbase\ttfonts.py", line 161, in TTFOpenFile raise TTFError('Can\'t open file "%s"' % fn) reportlab.pdfbase.ttfonts.TTFError: Can't open file "Fonts/ipaexg.ttf"
どこを直せばいいんでしょうか、教えて下さい。
こちらの質問が複数のユーザーから「調査したこと・試したことが記載されていない質問」という指摘を受けました。
FileNotFoundError: [Errno 2] No such file or directory: 'Fonts/ipaexg.ttf'
と表示されていますので、Fonts/ipaexg.ttf が無いのでしょう。
IPAex フォントをインストールしていない場合は、
https://ja.osdn.net/projects/ipafonts/downloads/49986/IPAexfont00103.zip/
などからダウンロードするとよいかと思います。
とりあえずDLしたんですが、ipaexgとか、
これ、どこに移動すればいいんでしょうか?
どこでもよいので適当にディレクトリを作ってそこに入れておいて、registerFont の第2引数に TTF ファイルの絶対パスを指定してみて下さい。
pdfmetrics.registerFont(TTFont("IPAexg", 絶対パスで指定))
pdfmetrics.registerFont(TTFont("IPAexm", 絶対パスで指定))
Python3.9と同じディレクトリにFonts作ってそこにttf2つぶっこんでもできなかったので絶対パスにします。
ところで、このコードを改良したい旨の質問を、一応新しい質問として2つ立てたのですが、ここで聞いたほうがいいんでしょうか?
新規の質問の方がよろしいかと思います。
やはりそうですか、新規で2つ質問したので、宜しくお願いします、
英単語帳作成phase1と2というやつです。
回答2件