gpt4 book ai didi

python - 从 svg 转换为 pdf

转载 作者:太空狗 更新时间:2023-10-30 01:01:40 25 4
gpt4 key购买 nike

我正在尝试使用以下代码将 svg 文件转换为 pdf,但出现错误

来源:

from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF
drawing = svg2rlg("test.svg")
renderPDF.drawToFile(drawing, "test.pdf")

错误:

pydev debugger: starting
Traceback (most recent call last):
File "C:\Users\Stone\.eclipse\org.eclipse.platform_4.3.0_1709980481_win32_win32_x86\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1446, in <module>
debugger.run(setup['file'], None, None)
File "C:\Users\Stone\.eclipse\org.eclipse.platform_4.3.0_1709980481_win32_win32_x86\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1092, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "C:\Users\Stone\Documents\FastDose\src\Beagle\Prove e test\test_renderpdf.py", line 9, in <module>
drawing = svglib.svg2rlg("test.svg")
File "C:\Python27\lib\site-packages\svglib\svglib.py", line 1178, in svg2rlg
svgRenderer.render(svg)
File "C:\Python27\lib\site-packages\svglib\svglib.py", line 511, in render
self.render(child, self.mainGroup)
File "C:\Python27\lib\site-packages\svglib\svglib.py", line 534, in render
item = self.renderG(n)
File "C:\Python27\lib\site-packages\svglib\svglib.py", line 626, in renderG
item = self.render(child, parent=gr)
File "C:\Python27\lib\site-packages\svglib\svglib.py", line 534, in render
item = self.renderG(n)
File "C:\Python27\lib\site-packages\svglib\svglib.py", line 626, in renderG
item = self.render(child, parent=gr)
File "C:\Python27\lib\site-packages\svglib\svglib.py", line 534, in render
item = self.renderG(n)
File "C:\Python27\lib\site-packages\svglib\svglib.py", line 626, in renderG
item = self.render(child, parent=gr)
File "C:\Python27\lib\site-packages\svglib\svglib.py", line 554, in render
shape = getattr(self.shapeConverter, methodName)(n)
File "C:\Python27\lib\site-packages\svglib\svglib.py", line 875, in convertPath
normPath = normaliseSvgPath(d)
File "C:\Python27\lib\site-packages\svglib\svglib.py", line 146, in normaliseSvgPath
a = string.replace(a, 'e-', 'ee')
NameError: global name 'string' is not defined

此外,我想避免硬文件并使用 StringIO 创建临时 svg/pdf 文件,但我猜 svg2rlg 只接受路径作为输入。

最佳答案

为了让它工作,我手动编辑了:

/Users/<MY_USER>/.virtualenvs/<MY_VENV>/lib/python2.7/site-packages/svglib/svglib.py

并添加这个缺失

导入字符串

关于python - 从 svg 转换为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23345913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com