Python:导入文档错误:“从异常导入PendingDeprecationWarning“

xfyts7mz  于 2023-02-01  发布在  Python
关注(0)|答案(1)|浏览(278)

我想把多个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*
kb5ga3dv

kb5ga3dv1#

我发现B Remmelzwaal解决方案非常好。试试这个。你还必须安装这个库:

pip install python-docx

相关问题