如何使用docx packag编辑文本

2024-05-08 22:52:58 发布

您现在位置:Python中文网/ 问答频道 /正文

目标:我想从word文件中读取文本,然后将每个字符的ascii值增加一个预定义的数字(某种编码),并将其保存到同一个文件中。例如:“A”的ascii是65,所以我需要它变成75。我正在写下面的代码,在这一点上是卡住了。 `你知道吗

import docx
from docx import Document
data = Document("C:\Python27\Testing.docx")
for n in data.paragraphs:
    temp= n.text
for d in temp:
    try:
        temp1 = str(temp)
    except UnicodeEncodeError:
        temp1 = temp.encode('ascii','replace')
        pass
print temp1

现在我得到的输出是这样的

This is just a test of what I?m gonna make. Fingers crossed?

原来的字符串是

This is just a test of what I’m gonna make. Fingers crossed…

如何用相应的ascii字符替换Unicode字符以便继续?请提供一些建议。你知道吗


Tags: 文件intestimportfordataisascii

热门问题