python open r/rb

 Python 3 环境下:

  • r:Python 将会按照编码格式进行解析,read() 操作返回的是str

在windows下,打开文本文件时,Python3将本来应该读入的换行符\r\n处理成\n,方便我们处理。

当我们将\n写入文件时,python也会默认将其替换成\r\n。

  • rb:也即 binary mode,read()操作返回的是bytes

​​​​​​​在学习python网络编程时,读取.html文件时使用的是'rb'.

修改后的新代码遇到报错: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/lib/utils.py", line 523, in open_for_read return open_for_read_by_name(name,mode) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/lib/utils.py", line 463, in open_for_read_by_name return open(name,mode) FileNotFoundError: [Errno 2] No such file or directory: 'SimSun.ttf' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/lib/utils.py", line 530, in open_for_read return BytesIO((datareader if name[:5].lower()=='data:' else rlUrlRead)(name)) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/lib/utils.py", line 476, in rlUrlRead return urlopen(name).read() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 216, in urlopen return opener.open(url, data, timeout) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 503, in open req = Request(fullurl, data) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 322, in __init__ self.full_url = url File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 348, in full_url self._parse() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 377, in _parse raise ValueError("unknown url type: %r" % self.full_url) ValueError: unknown url type: 'SimSun.ttf' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/pdfbase/ttfonts.py", line 159, in TTFOpenFile f = open_for_read(fn,'rb') File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/lib/utils.py", line 534, in open_for_read return open_for_read(name,mode) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/lib/utils.py", line 532, in open_for_read raise IOError('Cannot open resource "%s"' % name) OSError: Cannot open resource "SimSun.ttf" During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/bravesword/Desktop/legal soup/test1.py", line 164, in <module> save_text_to_pdf(extracted_text, output_pdf) File "/Users/bravesword/Desktop/legal soup/test1.py", line 117, in save_text_to_pdf pdfmetrics.registerFont(TTFont('SimSun', 'SimSun.ttf')) # 确保字体文件路径正确 File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/pdfbase/ttfonts.py", line 1207, in __init__ self.face = TTFontFace(filename, validate=validate, subfontIndex=subfontIndex) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/pdfbase/ttfonts.py", line 1088, in __init__ TTFontFile.__init__(self, filename, validate=validate, subfontIndex=subfontIndex) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/pdfbase/ttfonts.py", line 447, in __init__ TTFontParser.__init__(self, file, validate=validate,subfontIndex=subfontIndex) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/pdfbase/ttfonts.py", line 183, in __init__ self.readFile(file) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/pdfbase/ttfonts.py", line 259, in readFile self.filename, f = TTFOpenFile(f) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/reportlab/pdfbase/ttfonts.py", line 169, in TTFOpenFile raise TTFError('Can\'t open file "%s"' % fn) reportlab.pdfbase.ttfonts.TTFError: Can't open file "SimSun.ttf"
最新发布
08-03
<think>我们正在解决ReportLab中注册字体时出现的“Can't open file SimSun.ttf”错误。这个错误通常是由于字体文件路径不正确或文件不可访问导致的。以下是详细的解决步骤: ### 步骤1:确认字体文件路径 首先,确保提供的字体文件路径是正确的。如果字体文件不在当前工作目录,需要使用绝对路径。例如: ```python # 使用绝对路径指定字体文件 pdfmetrics.registerFont(TTFont('SimSun', '/absolute/path/to/SimSun.ttf')) ``` ### 步骤2:检查文件权限 确保程序有权限读取该字体文件。在Linux/macOS上,可以使用`ls -l`命令检查权限;在Windows上,右键点击文件查看属性中的安全设置。 ### 步骤3:验证字体文件有效性 手动检查字体文件是否能正常打开(例如双击尝试在系统中打开)。如果文件损坏,需要重新获取。 ### 步骤4:使用系统字体路径(推荐) 如果字体已安装在系统中,可以直接使用系统字体路径。常见系统字体路径: - **Windows**: `C:/Windows/Fonts/simsun.ttc` (注意:SimSun在Windows上通常是ttc格式) - **macOS**: `/Library/Fonts/` 或 `~/Library/Fonts/` - **Linux**: `/usr/share/fonts/` 或 `~/.fonts/` ### 步骤5:处理字体格式 注意:SimSun字体在Windows上通常是`.ttc`(TrueType Collection)格式,而`TTFont`默认只支持`.ttf`。但ReportLab的`TTFont`实际上支持TTC文件,只要指定索引(因为TTC是多个字体的集合)。例如: ```python # 对于TTC文件,需要指定字体索引(通常0为常规,1为粗体等) pdfmetrics.registerFont(TTFont('SimSun', 'simsun.ttc', index=0)) ``` ### 完整修复代码示例 ```python from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas def save_text_to_pdf(text, output_path): try: # 尝试注册字体,使用绝对路径 # 注意:在Windows上,SimSun通常为ttc格式,且路径为C:/Windows/Fonts/simsun.ttc font_path = r'C:\Windows\Fonts\simsun.ttc' # Windows路径示例 # 如果是macOS/Linux,可能需要其他路径,例如:'/Library/Fonts/SimSun.ttf' pdfmetrics.registerFont(TTFont('SimSun', font_path, index=0)) except Exception as e: print(f"注册字体失败: {e}") # 可以尝试回退到其他字体 # 或者抛出异常 # 继续生成PDF... c = canvas.Canvas(output_path, pagesize=letter) # ...(其余代码不变) ``` ### 注意事项 1. **跨平台路径处理**:使用`os.path`模块处理路径可提高跨平台性: ```python import os font_path = os.path.join('C:', 'Windows', 'Fonts', 'simsun.ttc') ``` 2. **异常处理**:注册字体时捕获异常,避免程序崩溃。 3. **备用字体**:注册失败时提供备用方案(如使用内置字体或提示用户)。 ### 常见错误排查 - **文件扩展名错误**:确保扩展名正确(注意Windows默认隐藏扩展名,实际文件可能是`simsun.ttc`而非`simsun.ttf`)。 - **字体索引**:TTC文件需要指定索引,可通过字体查看工具确认索引号。 - **路径转义**:Windows路径中的反斜杠应使用原始字符串(`r'C:\...'`)或双反斜杠(`'C:\\...'`)。 ### §§相关问题§§ 1. 如何在ReportLab中注册`.ttc`格式的字体? 2. ReportLab如何跨平台处理字体路径? 3. 如何在PDF生成失败时提供友好的用户提示? 4. 如何检查ReportLab中字体是否注册成功? 5. 除了SimSun,还有哪些免费可用的中文字体适合PDF生成?</think>### 解决 ReportLab 注册字体时 "Can't open file SimSun.ttf" 错误 当使用 `TTFont` 注册字体时出现 "Can't open file" 错误,通常由以下原因导致: #### 1. 字体文件路径错误(最常见) ```python # 错误:相对路径不正确 pdfmetrics.registerFont(TTFont('SimSun', 'SimSun.ttf')) # 可能找不到文件 # 正确:使用绝对路径 from pathlib import Path font_path = Path(__file__).parent / 'fonts' / 'SimSun.ttf' # 推荐 pdfmetrics.registerFont(TTFont('SimSun', str(font_path))) ``` #### 2. 字体文件格式问题 ```python # SimSun 实际是 TTC 格式(TrueType Collection),需特殊处理 from reportlab.pdfbase.ttfonts import TTFont pdfmetrics.registerFont(TTFont('SimSun', 'simsun.ttc', index=0)) # 指定索引 ``` #### 3. 文件权限问题 ```python import os print(os.access('SimSun.ttf', os.R_OK)) # 检查读取权限 ``` #### 4. 完整解决方案(跨平台) ```python from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont import os import sys def register_chinese_font(): """自动处理不同系统的中文字体路径""" font_name = 'SimSun' # 尝试不同系统的默认字体路径 font_paths = [ # Windows r'C:\Windows\Fonts\simsun.ttc', # macOS '/Library/Fonts/SimSun.ttf', # Linux '/usr/share/fonts/truetype/simsun.ttf', # 当前目录 os.path.join(os.path.dirname(__file__), 'fonts', 'SimSun.ttf') ] for path in font_paths: if os.path.exists(path): try: # 处理 TTC 格式 if path.endswith('.ttc'): pdfmetrics.registerFont(TTFont(font_name, path, index=0)) else: pdfmetrics.registerFont(TTFont(font_name, path)) print(f"成功注册字体: {path}") return True except Exception as e: print(f"字体注册失败: {e}") print("错误: 未找到SimSun字体文件") # 回退到默认字体 pdfmetrics.registerFont(TTFont('Fallback', 'Helvetica')) return False # 使用示例 register_chinese_font() ``` #### 5. 验证字体是否注册成功 ```python # 检查已注册字体 print(pdfmetrics.getRegisteredFontNames()) # 应包含 'SimSun' # 创建测试文本 c = canvas.Canvas("font_test.pdf") c.setFont("SimSun", 12) c.drawString(100, 100, "中文测试") c.save() ``` ### 关键注意事项 1. **Windows 字体路径**:使用原始字符串避免转义问题 `r'C:\Windows\Fonts\simsun.ttc'` 2. **TTC 文件处理**:必须添加 `index` 参数指定字体索引 3. **文件扩展名**:Windows 系统实际是 `.ttc` 而非 `.ttf` 4. **权限检查**:确保 Python 进程有文件读取权限 5. **错误处理**:添加异常捕获和回退机制 ### 字体文件查找指南 | 系统 | 默认路径 | 命令 | |-----------|-----------------------------------|---------------------| | **Windows** | `C:\Windows\Fonts\` | 文件资源管理器直接查看 | | **macOS** | `/Library/Fonts/` | `ls /Library/Fonts/*.ttc` | | **Linux** | `/usr/share/fonts/truetype/` | `fc-list : family` | > 实际使用中,建议将字体文件放在项目目录的 `fonts/` 子目录下,使用相对路径引用[^4]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值