Read a docx document with python3
Solvedmamiemando Posted messages 33540 Registration date Status Modérateur Last intervention -
Hello,
I'm also starting out with doc, I want to read a Word file in Python:
import docx doc = docx.Document("L'ANGE GABRIEL") print (read(doc)) ModuleNotFoundError: No module named 'docx'
Where is the error? Thank you.
17 réponses
Hello,
I’m marking the topic as resolved (see #65), @quentin2121 StatutMembre is thinking about doing this in the future as explained here.
Thanks to everyone (especially 633049 and @Diablo76 StatutMembre) who helped quentin2121 find the answer. Below is a summary of this long discussion.
Summary of the problem
Reading a docx file in python3 on Windows.
Summary of the solution
- Install pycharm.
- Create a new project in pycharm.
- It is recommended to create a virtual environment for the project using the python 3.11 interpreter, as python 3.12 is currently causing issues.
- If python3.11 is not installed, see #59 for instructions.
- Install python-docx. Be careful not to install docx, but python-docx this link). Two methods are possible
- via the Python Package menu in pycharm
- via the terminal (shell) (not in the python interpreter!) of pycharm, by invoking pip yourself:
-
python -m pip install --upgrade pip # Upgrading pip pip install python-docx # Installing the package
-
- If there is a pip error, particularly:
When import docx in python3.3 I have error ImportError: No module named 'exceptions'
... you should consider using another version of python, such as python 3.11. The above error currently affects python3.12.
Good luck and thanks to everyone
In the bottom right corner of Pycharm, you can see the interpreter in use (in my screenshot 3.11)
You click on it and you can see all the installed interpreters (in my screenshot 3.9 and 3.11) as well as menus to configure everything.
There, either you choose 3.11, or you install it if it hasn't been done, and that will be fine at least for your current project.

As we have already told you, Pycharm creates a virtual environment for each project. All the dependencies of the project are in this environment (For example, docx if you install it, will only be accessible to this project, from another one you would have to install it again).
But there are settings that are global to Pycharm, and I don't know this IDE well enough to tell you whether the interpreter is global or not.
But since it says Python 3.11 (SAGC) which is the name of my project, I tend to think it is not a global setting.
When I was little, the Dead Sea was only sick.
George Burns
import docx doc = docx.Document # doc.read("THE ANGEL GABRIEL") In fact, the error may be that my document is on drive D, how can I access it from Python?
By entering cd dir on line 2?
I added my document in PythonProject via the Windows file explorer on drive C, it doesn't work any better.
In my courses, the docx module is already installed in PyCharm, as we don't add it at the start of the exercise.
Hello,
No matter where the file is located, put the path:
import docx doc = docx.Document("C:\\Phil\\Dev\\Python\\tests\\Word_test.docx") for para in doc.paragraphs: print('Paragraph:\n',para.text)
import docx doc = docx.Document("C:\\Users\\quent\\PycharmProjects\\pythonProject\\L'ANGE_GABRIEL.docx") I have this error:
doc = docx.Document("C:\\Users\\quent\\PycharmProjects\\pythonProject\\L'ANGE_GABRIEL.docx")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
I did this, but I'm still getting errors
I was taught to do it like this:
import docx
doc = docx.Document()
# doc.add_paragraph("hello word")
doc.save("hello word.docx")
paraObj1 = doc.add_paragraph("second paragraph")
paraObj2 = doc.add_paragraph("third paragraph")
paraObj1.add_run("additional text")
doc.save("multiple_paragraphs.docx")
Good evening,
In programming, when you have errors, you need to indicate these errors. Python is great for that, so please post the complete traceback of the errors, and especially no images, plain text is more than sufficient.
You should probably follow a tutorial regarding this module because right now, you seem to be completely struggling.
For example, this one:
https://www.geeksforgeeks.org/python-working-with-docx-module/
(there are other tutorials at the bottom of the page regarding this module)
Or even:
https://stackabuse.com/reading-and-writing-ms-word-files-in-python-via-python-docx-module/
Good luck.
Hello,
Have you tried this:
import docx doc = docx.Documents("C:/Users/quent/PycharmProjects/pythonProject/L'ANGE_GABRIEL.docx")
File "C:\Users\quent\PycharmProjects\pythonProject\l'ange gabriel.py", line 1, in <module>
import docx
File "C:\Users\quent\PycharmProjects\pythonProject4\venv\Lib\site-packages\docx.py", line 21, in <module>
from PIL import Image
File "C:\Users\quent\PycharmProjects\pythonProject4\venv\Lib\site-packages\PIL\Image.py", line 39, in <module>
import tempfile
File "C:\Users\quent\AppData\Local\Programs\Python\Python312\Lib\tempfile.py", line 45, in <module>
from random import Random as _Random
File "C:\Users\quent\PycharmProjects\pythonProject\random.py", line 2
generate a random number(1-10)
^^
SyntaxError: invalid syntax
I’m starting to wonder if I have the docx module installed on Pycharm? What path or console command can I use to check it, please?
Hello,
Is there no one to help me? I'm stuck here.
Small reminders:
- We are volunteers
- Our lives take precedence over any help on the forum
- When we ask you questions to better understand your problem, you don't have to respond quickly; when you do respond...
So if you want to continue receiving help from us, a little patience and respect are required.
A priori yes, the module is installed in your project's virtual environment; we can see in the call stack that the error goes through
File "C:\Users\quent\PycharmProjects\pythonProject4\venv\Lib\site-packages\docx.py",
And if we continue to follow the stack, it intends to use the Random module.
I haven't checked and I don't have time right now, but the call path surprises me.
Did you perhaps make the poor decision to write your own random module, with a bug in it?
On my PC that has Pycharm, I do have 2 files named random.py but
- they are not organized in a folder that looks like a personal folder
- the comments are not in French.
I therefore reformulate my question, did you write the file "C:\Users\quent\PycharmProjects\pythonProject\random.py"
When I was little, the Dead Sea was only sick.
George Burns
So for some reason I don't know, PyCharm thinks the random module is your file and not the original one.
We need to make sure the real one is taken into account, maybe reinstall it.
As for
I only copied the beginning of the code, I have an error with "syntax error" on line 2.
yes it is doubly obvious, first because in your other program that is exactly what the error message is telling you. And moreover, a comment starts with #.
When I was young, the Dead Sea was just sick.
George Burns
I deleted the file Random.py in my Windows explorer (after backing it up elsewhere), but while testing my code L'ANGE_GABRIEL.py, there are still errors:
File "C:\Users\quent\PycharmProjects\pythonProject\L'ANGE_GABRIEL.py", line 1, in <module>
import docx
File "C:\Users\quent\PycharmProjects\pythonProject4\venv\Lib\site-packages\docx.py", line 30, in <module>
from exceptions import PendingDeprecationWarning
ModuleNotFoundError: No module named 'exceptions'
By the way, what command should I use in the console to reinstall Random?
Hello,
I haven't read everything, so my apologies if what I'm saying is a repeat:
- As explained in this link, for modern versions of Python, you need to install python-docx and not docx. This will solve the error you mentioned in #28.
pip remove docx pip install python-docx
- Regarding #26 and the naming of your function
- A Python function name should not contain spaces.
- I strongly discourage the use of non-ASCII characters (typically accented characters). So rename your function "générer un nombre aléatoire" to "generer_un_nombre_aleatoire". If you really wanted to use Unicode characters, your file would need to start with the appropriate header. Example:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- def f(): print("Hello world") - I recommend naming your functions in English; this helps develop good habits for the future and eliminates the use of accented characters by default.
- In your particular case, the function you are trying to implement is probably random.randint.
from random import randint n = randint(1, 10) # 1 <= n <= 10
- Instead of taking screenshots of your call stack, copy-paste it in full (as you did in #28). Also, please include it in a code section as explained here.
Good luck
File "C:\Users\quent\AppData\Local\Programs\Python\Python312\Lib\code.py", line 63, in runsource code = self.compile(source, filename, symbol) File "C:\Users\quent\AppData\Local\Programs\Python\Python312\Lib\codeop.py", line 153, in __call__ return _maybe_compile(self.compiler, source, filename, symbol) File "C:\Users\quent\AppData\Local\Programs\Python\Python312\Lib\codeop.py", line 73, in _maybe_compile return compiler(source, filename, symbol)
pip list:
File "C:\Users\quent\AppData\Local\Programs\Python\Python312\Lib\code.py", line 63, in runsource code = self.compile(source, filename, symbol) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\quent\AppData\Local\Programs\Python\Python312\Lib\codeop.py", line 153, in __call__ return _maybe_compile(self.compiler, source, filename, symbol) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\quent\AppData\Local\Programs\Python\Python312\Lib\codeop.py", line 73, in _maybe_compile return compiler(source, filename, symbol) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\quent\AppData\Local\Programs\Python\Python312\Lib\codeop.py", line 118, in __call__ codeob = compile(source, filename, symbol, self.flags, True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<input>", line 1 pip list ^^^^
Maybe Pycharm is bugging, maybe it needs to be uninstalled, reinstalled? While saving my projects?
I opened a project in PyCharm where I have docx.
With the package manager (as @Diablo76 showed you), I can see it in the list of installed components.

Do you see it?
I'm asking you this because earlier your error trace seemed to go through this package. So maybe it wasn't the right package as @mamiemando suggested.
In that case, maybe you should uninstall it first.
To the right of the package manager, you can click on the three dots.
When I run pip list in the terminal, I get:
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "C:\Users\quent\PycharmProjects\pythonProject1\venv\Scripts\pip.exe\__main__.py", line 4, in <module>
File "C:\Users\quent\PycharmProjects\pythonProject1\venv\Lib\site-packages\pip\_internal\cli\main.py", line 9, in <module>
from pip._internal.cli.autocompletion import autocomplete
File "C:\Users\quent\PycharmProjects\pythonProject1\venv\Lib\site-packages\pip\_internal\cli\autocompletion.py", line 10, in <module>
from pip._internal.cli.main_parser import create_main_parser
File "C:\Users\quent\PycharmProjects\pythonProject1\venv\Lib\site-packages\pip\_internal\cli\main_parser.py", line 8, in <module>
from pip._internal.cli import cmdoptions
File "C:\Users\quent\PycharmProjects\pythonProject1\venv\Lib\site-packages\pip\_internal\cli\cmdoptions.py", line 23, in <module>
from pip._internal.cli.parser import ConfigOptionParser
File "C:\Users\quent\PycharmProjects\pythonProject1\venv\Lib\site-packages\pip\_internal\cli\parser.py", line 12, in <module>
from pip._internal.configuration import Configuration, ConfigurationError
File "C:\Users\quent\PycharmProjects\pythonProject1\venv\Lib\site-packages\pip\_internal\configuration.py", line 20, in <module>
from pip._internal.exceptions import (
File "C:\Users\quent\PycharmProjects\pythonProject1\venv\Lib\site-packages\pip\_internal\exceptions.py", line 7, in <module>
from pip._vendor.pkg_resources import Distribution
File "C:\Users\quent\PycharmProjects\pythonProject1\venv\Lib\site-packages\pip\_vendor\pkg_resources\__init__.py", line 2164, in <module>
register_finder(pkgutil.ImpImporter, find_on_path)
^^^^^^^^^^^^^^^^^^^
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
The problem seems to be related to python3.12 (see here). This discussion suggests reverting to either python3.11 or adopting this solution.
Thank you to Whismeril, goulu, and mamiemando for taking an interest in my problem. I uninstalled Python 3.12 and installed Python 3.11. When I run pip install python-docx, I get:
(venv) PS C:\Users\quent\PycharmProjects\pythonProject> pip install python-docx
No Python at '"C:\Users\quent\AppData\Local\Programs\Python\Python312\python.exe'
(venv) PS C:\Users\quent\PycharmProjects\pythonProject>
Python is installed in the C of app data, local, during the install, at the very beginning, I checked "install the path", at the bottom, an option, could this be the issue?
And if you copy and paste the path into the text area of the explorer window?
When I was little, the Dead Sea was only sick.
George Burns
Hello Mamiemando,
Thanks for closing the topic. But I hadn't done it because I still have questions. I installed pyinstaller on my PC to run Python projects on any PCs not equipped with Python. It works for other files, but not for the ange_gabriel?
Actually, it opens fine in Pycharm with the lines I included in the project, but I wanted all the content of the text file to open in the Pycharm execution console! Then on a PC after I have directed it to pyinstaller.
Do I need to add any other lines of code for it to display in Pycharm? Will it be possible to create a .exe file with pyinstaller? Thanks again for the help!
Hello quentin2121: this question is not directly related to the initial topic, so you should ask your question in a new discussion if you want more details. But in essence, either the target machine has a Python interpreter and the necessary dependencies for the topic, or you need to transform your project into a "stand alone" executable, for example with pyinstaller.
-
Texture issues in enshrouded
à 19:16 -
Edit ad on leboncoin for free
à 08:01 -
Disable hard drive password
le 25 May -
Duration of unpaid filing with telephone operators
le 25 May -
Where to rewatch temptation island...
le 25 May -
Black screen at wakfu launch
le 25 May -
My pc starts up correctly but my screen won't turn on: what should i do?
le 25 May -
Voicemail icon that remains displayed
le 25 May -
Is onfancy.fr reliable?
le 25 May -
Is the sports shoes website reliable?
le 25 May














