lst0426 2022-08-22 06:39 采纳率: 16.7%
浏览 42

python存储TXT为Word

我想要达到的结果

All strings must be XML compatible: Unicode or
ASCII, no NULL bytes or control characters
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\docx\oxml\xmlchemy.py", line 273, in _add_child
setattr(child, key, value)
File "src\lxml\etree.pyx", line 1041, in lxml.etree._Element.text.set
File "src\lxml\apihelpers.pxi", line 748, in lxml.etree._setNodeText
File "src\lxml\apihelpers.pxi", line 736, in lxml.etree._createTextNode
File "src\lxml\apihelpers.pxi", line 1541, in lxml.etree._utf8
ValueError: All strings must be XML compatible: Unicode or
ASCII, no NULL bytes or control characters


```python
from docx import Document

#获取本文档中的所有章节
document = Document()
f = open("./data/W.OUT","r",encoding='gbk')   # 注意编码类型
line = f.readline()
while line:
#这行有错误
  document.add_paragraph(line)
#document.add_paragraph(line.decode('utf-8'))

  line = f.readline()
 
f.close()

```

  • 写回答

3条回答 默认 最新

  • 「已注销」 2022-08-22 07:58
    关注

    报错复制发过来

    评论

报告相同问题?

问题事件

  • 修改了问题 8月22日
  • 修改了问题 8月22日
  • 创建了问题 8月22日