pypandoc: Windows html to docx fails to embed images in the docx file

I have an html file which links to an image in the same folder, when converting from html to docx on windows it throws the error [WARNING] Could not fetch resource test.png: PandocResourceNotFound "test.png"

pypandoc-binary==1.10

html file:

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Test Title</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">

<body>

  <h1 class="section">Test Heading</h1>

  <div class="row">
    <img src="test.png" alt="test alt" />
  </div>

</body>

</html>

python script

import pypandoc
 
pypandoc.convert_file(
    'index.html',
    to='docx',
    format='html',
    outputfile='test.docx',
)

output of python test.py: [WARNING] Could not fetch resource test.png: PandocResourceNotFound "test.png"

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (10 by maintainers)

Commits related to this issue

Most upvoted comments

@JessicaTegner thanks for the prompt response. While I’m no expert on the implications of the options you provided, I don’t think it’s unreasonable to have sandbox=False by default as this would replicate the pandoc CLI usage more closely and avoid confusion.

Namely, when using pandoc directly one would have to explicitly provide --sandbox as a parameter in order to run in a sandbox mode, so the same can be true for pypandoc by explicitly requiring users to specify sandbox=True to get the sandbox effect. This way, if the users “go out of their way” to override the default value of the sandbox parameter, then they would have presumably read pandoc’s documentation and know that they need to use embed_data_files option along with it (e.g. for conversion to docx), which should hopefully avoid errors such as the one in this issue.

In either case, more thorough documentation is needed, especially if we keep sandbox=True by default.

I had the same issue and passing sandbox=False (default is True) fixed it for me. So in your case it’d be

pypandoc.convert_file(
    'index.html',
    to='docx',
    format='html',
    outputfile='test.docx',
    extra_args=extra_args,
    sandbox=False,  # <----------add this
)

@JessicaTegner It seems setting sandbox=False is not recommended in most cases, based on the docstring :param bool sandbox: Run pandoc in pandocs own sandbox mode, limiting IO operations in readers and writers to reading the files specified on the command line. Anyone using pandoc on untrusted user input should use this option. Note: This only does something, on pandoc >= 2.15 . Do you have suggestions on how to avoid having to set sandbox to False and still have images working as expected?