Click here to Skip to main content
15,851,387 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So i just installed pdf2docx and was going to try it out. Couldn't write a single line of code before getting errors all over the place.
This my entire, ultra-complex code:
Python
from pdf2docx import parse

And it tells me:
ImportError: cannot import name 'Iterable' from 'collections' (C:\Users\spacemonkey\AppData\Local\Programs\Python\Python310\lib\collections\__init__.py)


UPDATE: I got it working, i just had to uninstall any trace of Python, then restart, then pip install pdf2docx again, then add the Script folder that got installed to to the system PATH, then restart.

But now, new problem. This is the code now:
Python
from pdf2docx import parse
from typing import Tuple

def convert(input_file: str, output_file: str, pages: Tuple = None):
    if pages:
        pages = [int(i) for i in list(pages) if i.isnumeric()]
    result = parse(pdf_file=input_file, docx_file=output_file, pages=pages)
    summary = { "File": input_file, "Pages": str(pages), "Output File": output_file }
    print("## Summary ##################################################")
    print("\n".join("{}:{}".format(i, j) for i, j in summary.items()))
    print("#############################################################")
    return result

inp1 = str(input("Directory to PDF: "))
inp2 = str(input("Directory to save DOCX: "))
print(convert(input_file=inp1, output_file=inp2))


Running it, this is the entire console thing, with input and all:
Directory to PDF: "C:\Users\spacemonkey\Downloads\Elon Musk - Wikipedia.pdf"
Directory to save DOCX: "C:\Users\spacemonkey\Downloads\Elon Musk - Wikipedia.docx" 
mupdf: cannot open "C:\Users\spacemonkey\Downloads\Elon Musk - Wikipedia.pdf": Invalid argument
Traceback (most recent call last):
  File "c:\Users\spacemonkey\source\repos\SCRAM\python\pdftodocx.py", line 16, in <module>
    print(convert(input_file=inp1, output_file=inp2))
  File "c:\Users\spacemonkey\source\repos\SCRAM\python\pdftodocx.py", line 7, in convert
    result = parse(pdf_file=input_file, docx_file=output_file, pages=pages)
  File "C:\Users\spacemonkey\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pdf2docx\main.py", line 34, in convert
    cv = Converter(pdf_file, password)
  File "C:\Users\spacemonkey\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pdf2docx\converter.py", line 39, in __init__
    self._fitz_doc = fitz.Document(pdf_file)
  File "C:\Users\spacemonkey\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\fitz\fitz.py", line 3815, in __init__
    _fitz.new_Document(
RuntimeError: cannot open "C:\Users\spacemonkey\Downloads\Elon Musk - Wikipedia.pdf": Invalid argument
PS C:\Users\spacemonkey> 


What I have tried:

I haven't tried anything really, since i have no idea what so ever why this doesn't work. I assumed it would work since i just downloaded it, but it seems there is something more i need to install or import into the script to make it work.
Posted
Updated 9-Dec-21 9:09am
v2
Comments
Richard MacCutchan 9-Dec-21 10:33am    
See my updated solution below.
Richard MacCutchan 9-Dec-21 16:28pm    
I just tried your latest code and it works fine. You need to check whether that path name is actually where you have stored the file.
Member 14769677 9-Dec-21 19:40pm    
It is, i literally right-clicked the file and pressed "Copy as path"
Richard MacCutchan 10-Dec-21 4:20am    
Well it works fine here, I have run it a couple of times to make sure. I do not have "Copy as path" in my version of Explorer, is that provided by some extension package?

1 solution

There is something definitely wrong with your Python installation if you get that message. As I already suggested in your previous question, you need to check your installation folders to find out what is happening. Reposting these question will not change that advice.

[edit]
Looking at the information you provided in your previous question I think you may have two Python versions installed. So if you then install a package with one version it will not be available in the other. I suggest you remove all traces of both installations and start again with only the latest version.
[edit]
 
Share this answer
 
v2
Comments
Member 14769677 9-Dec-21 11:53am    
Okay thank you.
Member 14769677 9-Dec-21 11:55am    
Yeah i looked now in the Software and Programs or whatever the setting menu is called, and i had 3.9.5, 3.9.0, and 3.10 installed. Deleting and reinstalling now.
Member 14769677 9-Dec-21 12:26pm    
Okay so new problem, i got it working now but when i actually try to use it, it doesn't go very well. Meaning it doesn't work at all. I have added an input for PDF file and output for directory to save the docx file. Now it gives me the good old 'RuntimeError: cannot open "C:/Users/spacemonkey/Downloads/Elon Musk.pdf": Invalid argument'. What?
Richard MacCutchan 9-Dec-21 12:40pm    
Sorry, I have no idea what your code is doing. Please update your question with the complete details.
Member 14769677 9-Dec-21 17:31pm    
I have updated the question with the new code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900