我想把多个txt文件转换成docx。我用这个代码:
from docx import Document
import re
import os
path = 'd://2022_12_02'
direct = os.listdir(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')
运行后出现此错误:
Traceback (most recent call last):
File "D:\convert txt to docs.py", line 4, in <module>
from docx import Document
File "C:\Users\Castel\AppData\Roaming\Python\Python310\site-packages\docx.py", line 30, in <module>
from exceptions import PendingDeprecationWarning
ModuleNotFoundError: No module named 'exceptions'
>>>
另外,在docx模块中,我看到这一行带有红色下划线:
- 从例外导入PendingDeprecationWarning*
1条答案
按热度按时间kb5ga3dv1#
我发现B Remmelzwaal解决方案非常好。试试这个。你还必须安装这个库: