我的问题与How to increase heap memory in pandoc execution?有关,但添加了一个特定于Python的组件。
背景:我试图自动生成科学报告。我已经将数据写入HTML文件,我希望使用Pandoc.exe (文件转换程序)将数据转换为.docx Word文档。我已经处理了一个带有图像、表格等的更小的HTML文件。那个文件是307 was。
当我试图转换一个更大的文件(~4.5MB)并嵌入多个图形时,问题就开始了。我一直使用pypandoc进行转换,如下所示:
import pypandoc
PANDOC_PATH = r"C:\Program Files\RStudio\bin\pandoc"
infile = savepath + os.sep + 'Results ' + name + '.html'
outfile = savepath + os.sep + 'Results ' + name + '.docx'
output = pypandoc.convert(source=infile, format='html', to='docx', \
outputfile=outfile, extra_args=["+RTS", "-K64m", "-RTS"])但我犯了很多错误。通常:
RuntimeError: Pandoc died with exitcode "2" during conversion:
b"Stack space overflow: current size 33692 bytes.\nUse `+RTS -Ksize -RTS' to increase it.\n"或者,如果我将-Ksize的值提高到256米,如下所示:
RuntimeError: Pandoc died with exitcode "1" during conversion: b'pandoc: out of memory\r\n'能解释一下发生了什么吗,在这里,有什么方法我可以克服这个困难?我想过的一个解决方案是让我的图像小得多。我刚刚缩小了(80-500‘s)原图,每幅图像的宽度和高度取决于它的原始尺寸:
data_uri = base64.b64encode(open(formats[graph][0], 'rb').read()).decode('utf-8')
img_tag = ('<img src="data:image/jpg;base64,{0}" height='+formats[graph][2][0]+'
width='+formats[graph][2][1]+'>').format(data_uri) 谢谢你的帮忙
发布于 2016-04-08 19:18:09
非常感谢user2407038在这个问题上的帮助!
两个修复程序最终允许我使用pypandoc将较大的HTML文件转换为docx文件。
第一个,如所建议的,是
增加堆的最大大小,例如将-M2GB添加到extra_args
这就是:
output = pypandoc.convert(source=infile, format='html', to='docx', outputfile=outfile, extra_args=["-M2GB", "+RTS", "-K64m", "-RTS"])
在增加堆大小之后,我仍然有第二个问题,所以我不确定解决方案是否有效。Python返回了如下错误消息:
Data.Text.Internal.Encoding.Fusion.streamUtf8: RuntimeError:在转换过程中,Pandoc使用了现有代码"1“死亡: b"pandoc:无法解码字节'\x91':UTF-8流\n无效”
它是通过更改html文件的打开方式来修正的。将encoding关键字参数设置为'utf8'允许转换到工作:
report = open(savepath + os.sep + 'Results ' + name + '.html', 'w', encoding='utf8')https://stackoverflow.com/questions/36481480
复制相似问题