pypandoc icon indicating copy to clipboard operation
pypandoc copied to clipboard

TypeError: 'WindowsPath' object is not iterable in ".convert_file()" method. Windows 10 x64

Open AleksVersus opened this issue 1 year ago • 8 comments

Hello. Sorry my bad english.

I install pypandoc by pip, i used the command:

pip install pypandoc_binary

But when i run simple script to convert file, this throws an exception. Script example:

import pypandoc
pypandoc.convert_file(os.path.abspath('00_01_start.txt'), to='markdown', format='dokuwiki', outputfile='00_01_start.md')

Exception in REPL:

Traceback (most recent call last):
  File "D:\my\projects\howdo_faq\[source]\wiki.qsp.org.mw\convert.py", line 18, in <module>
    main()
  File "D:\my\projects\howdo_faq\[source]\wiki.qsp.org.mw\convert.py", line 10, in main
    output = pypandoc.convert_file(os.path.abspath('00_01_start.txt'), to='markdown', format='dokuwiki', outputfile='00_01_start.md')
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aleks\AppData\Local\Programs\Python\Python312\Lib\site-packages\pypandoc\__init__.py", line 200, in convert_file
    return _convert_input(discovered_source_files, format, 'path', to, extra_args=extra_args,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aleks\AppData\Local\Programs\Python\Python312\Lib\site-packages\pypandoc\__init__.py", line 383, in _convert_input
    input_file = sorted(input_file)
                 ^^^^^^^^^^^^^^^^^^
TypeError: 'WindowsPath' object is not iterable

Exception in VS Code terminal:

Traceback (most recent call last):
  File "d:\my\projects\howdo_faq\[source]\wiki.qsp.org.mw\convert.py", line 18, in <module>
    main()
  File "d:\my\projects\howdo_faq\[source]\wiki.qsp.org.mw\convert.py", line 10, in main
    output = pypandoc.convert_file(os.path.abspath('00_01_start.txt'), to='markdown', format='dokuwiki', outputfile='00_01_start.md')      
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^      
  File "C:\Users\aleks\AppData\Local\Programs\Python\Python312\Lib\site-packages\pypandoc\__init__.py", line 195, in convert_file
    raise RuntimeError("source_file is not a valid path")
RuntimeError: source_file is not a valid path

Input File is exist. Path to input file is string.

I use:

Windows 10 Pro x64 22H2 Python: 3.12.5 Pypandoc: 2.19.2

If i use ".convert_text()" method, the exceptions do not appear.

What do I wrong?

AleksVersus avatar Sep 02 '24 09:09 AleksVersus

I look at the pypandoc's __init__.py. Method '_convert_input()'. Change line 383 by this line:

            input_file = [str(source)]

and script is work. But I don't know, where source parameter transform in WindowsPath, and and how will this affect the rest of the code.

AleksVersus avatar Sep 02 '24 09:09 AleksVersus

I look at the pypandoc's __init__.py. Method '_convert_input()'. Change line 383 by this line:

        input_file = [str(source)]

and script is work. But I don't know, where source parameter transform in WindowsPath, and and how will this affect the rest of the code.

I've also the same error and this is working. Thanks!!

mCrisanSph avatar Feb 18 '25 12:02 mCrisanSph

Out of the blue - this has become an issue for me too! I have no idea why.

I'm on Windows 11

SudoCerb avatar May 23 '25 11:05 SudoCerb

Seems to be an edge case - I will investigate further

SudoCerb avatar May 23 '25 12:05 SudoCerb

@AleksVersus or @SudoCerb I'm happy for a pr fixing this.

JessicaTegner avatar May 25 '25 12:05 JessicaTegner

Will try isolate the issue this week and at least give some feedback.

SudoCerb avatar May 25 '25 13:05 SudoCerb

Hi @JessicaTegner - in my instance it would appear that any file path that contains square brackets ([ or ]) would throw this error. I haven't yet stepped into pypandoc code to test yet

SudoCerb avatar May 28 '25 08:05 SudoCerb

I am seeing the exact same issue. My file also has "[" and "]" in its name.

This is how to reproduce this bug - MacOS in my case:

import pypandoc
local_path: str = '/Users/foo/[Draft]mydoc.doc'
html_text = pypandoc.convert_file(local_path, "html", format="docx", sort_files=False, extra_args=[ '--extract-media=/tmp'])

The buggy line: args += input_file

The exception: TypeError: 'PosixPath' object is not iterable

If you pass sort_files=True you will hit the bug in the subject.

Overall the code seems to want to convert a str path to Path, but later on tests again, etc - overall it gets confused and has these failure points.

Bottom line:

  • Path is not sortable, so you need to pass sort_files=False
  • [ and ] in the name mean that str(source) will also cause issues.

sglebs avatar Nov 28 '25 16:11 sglebs