Jan-29-2023, 09:13 AM
(This post was last modified: Jan-29-2023, 11:24 PM by Larz60+.
Edit Reason: fixed error tags
)
hello, I have an error on the code below. Ths code converts all txt files into docx, from the same folder. But I have an error on path, I try many combinations, how to write the path, but still doesn't work.
This is the code:
![[Image: A5MjLh.jpg]](https://snipboard.io/A5MjLh.jpg)
Can anyone test the code, and help me with a solution?
This is the code:
# pip install docx
# pip install document
# pip install python-docx
from docx import Document
import re
import os
path = r'd:\2022_12_02'
direct = os.listdir(path)
print(path)
for i in direct:
document = Document()
document.add_heading(i, 0)
myfile = open('d:\2022_12_02'+i).read()
myfile = re.sub(r'[^\x00-\x7F]+|\x0c',' ', myfile) # remove all non-XML-compatible characters
p = document.add_paragraph(myfile)
document.save('d:\2022_12_02'+i+'.docx')This is the error:Error:Traceback (most recent call last):
File "D:\convert txt to docs.py", line 16, in <module>
myfile = open('d:\2022_12_02'+i).read()
FileNotFoundError: [Errno 2] No such file or directory: 'd:\x822_12_025g.txt'
>>> ![[Image: A5MjLh.jpg]](https://snipboard.io/A5MjLh.jpg)
Can anyone test the code, and help me with a solution?
