我的问题与如何在pandoc执行中增加堆内存有关?,但添加了特定于Python的组件。
背景:我正在尝试自动生成科学报告。我已将数据写入HTML文件,并且想使用Pandoc.exe(文件转换程序)转换为.docx Word文档。我已经有了处理包含图片,表格等的较小HTML文件的过程。该文件为307KB。
当我尝试转换嵌入了多个图形的更大文件(〜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的值提高到256m,则如下所示:
RuntimeError: Pandoc died with exitcode "1" during conversion: b'pandoc: out of memory\r\n'
有人可以在这里解释发生了什么事,以及我可以解决该困难的某种方式吗?我考虑过的一种解决方案是使图像缩小很多。我一直在按比例缩小(80-500KB)原始图像,其中每个图像的宽度和高度取决于其原始尺寸:
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)
谢谢你的帮助
非常感谢user2407038对此的帮助!
最终,有两个修复程序使我可以使用以下命令将较大的HTML文件转换为docx文件pypandoc
:
如所建议的,第一个是
增加堆的最大大小,例如,将-M2GB添加到extra_args
那是:
output = pypandoc.convert(source=infile, format='html', to='docx', outputfile=outfile, extra_args=["-M2GB", "+RTS", "-K64m", "-RTS"])
增加堆大小后,我还有第二个问题,因此我不确定该解决方案是否有效。Python返回了这样的错误消息:
RuntimeError:转换期间Pandoc退出代码为“ 1”:b“ pandoc:无法解码字节'\ x91':Data.Text.Internal.Encoding.Fusion.streamUtf8:无效的UTF-8流\ n”
首先更改了html文件的打开方式即可修复该问题。将encoding
关键字参数设置为'utf8'
可以进行转换:
report = open(savepath + os.sep + 'Results ' + name + '.html', 'w', encoding='utf8')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句