2

pypandoc ( Pandocの python ラッパー)を使用して HTML 文字列を 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が初めてで、助けやヒントをいただければ幸いです。前もって感謝します!

4

2 に答える 2

0

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()
于 2014-12-30T22:39:22.020 に答える