我有4个函数应从特定的docx文件中获取一些突出显示的文本,scan4<colour>
是文本的变数,这些函数几乎相同,但是正在搜索和替换不同的突出显示的文本,它们打印出一样,但是他们不会替换文字
这些是4的2个函数,yellow
一个起作用,而green
一个不起作用
代码的作用是搜索并替换文本,然后加密,然后使用加密的字符串输入主文档,另一个则完全相同,但搜索不同的突出显示颜色。我尝试加密第一个功能,但第二个功能不能
此代码搜索黄色以对文本进行编码,然后将其替换,以便未加密的文档显示内容的加密字符串
def securi1_key():
file = open("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Keys/Securi1.key", "rb")
global key1
key1 = file.read()
file.close()
def rewrite_yellow():
securi1_key()
save_yellow_text()
# get key from file
file = open("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Keys/Securi1.key", "rb")
texty = scan4yellow.decode("utf-8")
encodedy = texty.encode()
# encrypt message
f = Fernet(key1)
encryptedy = f.encrypt(encodedy)
print(scan4yellow)
print(scan4yellow.decode("utf-8"))
document = docx.Document(f1)
for paragraph in document.paragraphs:
if scan4yellow.decode("utf-8") in paragraph.text:
inline = paragraph.runs
# loops for runs
for i in range(len(inline)):
if scan4yellow.decode("utf-8") in inline[i].text:
text = inline[i].text.replace(scan4yellow.decode("utf-8"), encryptedy.decode("utf-8"))
inline[i].text = text
document.save(f1)
def save_yellow_text():
securi1_key()
fp = f1
p = Path(fp)
filename1 = p.stem
storedtexty = filename1 + " Yellow Text"
storedtextencryptedy = storedtexty + ".encrypted"
list_of_files = os.listdir("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Text/")
if storedtexty in list_of_files:
storedtexty = (storedtexty + "1")
file = open("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Text/" + storedtexty, "w+")
file.write(scan4yellow.decode("utf-8"))
input_file1 = ("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Text/" + storedtexty)
if storedtextencryptedy in list_of_files:
storedtextencryptedy = (storedtextencryptedy + "1")
output_file1 = ("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Text/" + storedtextencryptedy)
with open(input_file1, "rb") as f:
data = f.read()
fernet = Fernet(key1)
encrypted = fernet.encrypt(data)
with open(output_file1, "wb") as f:
f.write(encrypted)
file.close()
os.remove(input_file1)
此代码应该完全相同,但是对于绿色:
def securi2_key():
file = open("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Keys/Securi2.key", "rb")
global key2
key2 = file.read()
file.close()
def rewrite_green():
securi2_key()
save_green_text()
# get key from file
file = open("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Keys/Securi2.key", "rb")
textg = scan4green.decode("utf-8")
encodedg = textg.encode()
print(encodedg)
# encrypt message
f = Fernet(key2)
encryptedg = f.encrypt(encodedg)
print(encryptedg)
document = docx.Document(f1)
for paragraph in document.paragraphs:
if scan4green.decode("utf-8") in paragraph.text:
inline = paragraph.runs
# loops for runs
for i in range(len(inline)):
if scan4green.decode("utf-8") in inline[i].text:
text = inline[i].text.replace(scan4green.decode("utf-8"), encryptedg.decode("utf-8"))
inline[i].text = text
document.save(f1)
def save_green_text():
securi2_key()
fp = f1
p = Path(fp)
filename2 = p.stem
storedtextg = filename2 + " Green Text"
storedtextencryptedg = storedtextg + ".encrypted"
list_of_files = os.listdir("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Text/")
if storedtextg in list_of_files:
storedtextg = (storedtextg + "1")
file = open("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Text/" + storedtextg, "w+")
file.write(scan4green.decode("utf-8"))
print(scan4green.decode("utf-8") + "tested1")
input_file2 = ("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Text/" + storedtextg)
if storedtextencryptedg in list_of_files:
storedtextencryptedg = (storedtextencryptedg + "1")
output_file2 = ("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Text/" + storedtextencryptedg)
with open(input_file2, "rb") as f:
data = f.read()
fernet = Fernet(key2)
encrypted = fernet.encrypt(data)
with open(output_file2, "wb") as f:
f.write(encrypted)
file.close()
os.remove(input_file2)
我应该用一些不连贯的字符串替换实际文本,并将实际文本加密后保存在另一个文件中,但是所有这些操作仅适用于第一个黄色功能,而不适用于绿色功能
理想情况下,它应该从读入的文件中提取文本,进行复制将其写到文件中并进行加密,然后从中获取加密字符串,然后将其替换为读入的文件中的文本,但是仅对于黄色代码有效,而绿色和几乎相同的其他代码无效
答案 0 :(得分:0)
我找到了一个答案,我所做的更改与我提供的代码无关,但是当我读到文本时,它是scan4green = (word.find(tag_t).text.encode('utf-8').lower())
。显然这导致它无法正常工作,但scan4yellow确实可以工作,因为我没有在末尾附加.lower()
。