Python 使用pypandoc将HTML字符串转换为LaTex
我正在尝试使用(的python包装器)将HTML字符串转换为LaTex 使用pypandoc转换文件效果很好:Python 使用pypandoc将HTML字符串转换为LaTex,python,pandoc,Python,Pandoc,我正在尝试使用(的python包装器)将HTML字符串转换为LaTex 使用pypandoc转换文件效果很好: import pypandoc input = 'SomeFile.html' output = pypandoc.convert(input, 'tex') 但是如果我尝试传递一些字符串(如果您定义字符串格式,根据pypandoc包索引,这应该是可能的),我会得到一个IOError:[Errno 63]文件名太长:: input = '''HTML-string''' outpu
import pypandoc
input = 'SomeFile.html'
output = pypandoc.convert(input, 'tex')
但是如果我尝试传递一些字符串(如果您定义字符串格式,根据pypandoc包索引,这应该是可能的),我会得到一个IOError:[Errno 63]文件名太长:
:
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新手,非常感谢您的帮助或提示。提前谢谢 如果您检查
pypandoc
源代码,您会看到convert
仅使用正确的输入和输出流运行pandoc
进程
当未找到
pandoc
命令时,会发生此错误。您可能安装了pypandoc
,而忘记了pandoc
本身。或者该命令不在shell路径中
如果有人需要答案,下面是一个使用子流程
模块的最小工作示例,从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()
您使用的是什么版本的
pypandoc
?嘿,您解决了问题吗?已安装了Pandoc。正如我所写的,如果我试图传递一个字符串,问题就会出现。转换文件工作正常。。。