我正在尝试使用pypandoc (Pandoc的python包装器)将HTML-string转换为LaTex。
使用pypandoc转换文件效果很好:
import pypandoc
input = 'SomeFile.html'
output = pypandoc.convert(input, 'tex')但是,如果我尝试传递一些字符串(根据pypandoc包索引,如果您定义字符串格式,这应该是可能的),我得到一个IOError: [Errno 63] File name too long:
input = '''HTML-string'''
output = pypandoc.convert(input, 'tex', format='html')不知怎么的,即使我指定了format='html',也需要一个文件。
我也尝试过使用StringIO模块来解决这个问题,但没有成功:
import pypandoc
import StringIO
output = StringIO.StringIO()
output.write('''HTML-string''')
contents = output.getvalue()
output.close()
convertedOutput = pypandoc.convert(contents, 'tex', format='html')我是python的新手,非常希望能得到一些帮助或提示。提前感谢!
发布于 2013-06-11 23:19:27
如果你检查一下pypandoc源代码,你会发现convert只是用适当的输入流和输出流运行pandoc进程。
找不到pandoc命令时会出现此错误。也许你安装了pypandoc,而忘记了pandoc本身。或者命令不在您的外壳PATH中。
发布于 2014-12-31 06:39:22
如果有人需要答案,这里是一个最小的工作示例,使用subprocess模块,从stdin读取输入,并在stdout上输出转换后的字符串。
# -*- coding: utf8 -*-
import subprocess
import os
PANDOC_PATH = r"path/to/pandoc"
def convert(text_to_convert):
pandoc = subprocess.Popen([os.path.join(PANDOC_PATH, 'pandoc.exe'), '-f', 'html', '-t', 'latex'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = pandoc.communicate(text_to_convert.encode('utf-8'))
converted_output = output
return converted_output.decode()https://stackoverflow.com/questions/17030391
复制相似问题