[네트워크 보안으로 파충류 연습 - 100가지 연습] 연습 20: 데이터 처리 - 지정된 파일 위치에 쓰기

목차

1. 목표 1: 디코딩 + 라벨링 해제

2. 목표 2: 태그 내용 추출

3. 목표 3: 처리된 데이터를 원래 위치에 삽입

4. 목표 4: 지정된 위치에 지정된 콘텐츠를 삽입합니다.

5. 목표 5: 상황에 맞는 글꼴 형식 지정

6. 목표 6: 다양한 문자열을 여러 위치에 삽입

7. 목표 7: 다양한 문자열을 여러 위치에 삽입

8. 목표 8: 지정된 문자열 뒤에 그래픽과 텍스트 쓰기


1. 목표 1: 디코딩 + 라벨링 해제

사용 기능: html.unescape() 디코딩 + 교체() 교체

import html

data = '\u003cp\u003e(此处忽略一万个字)'

# 解码HTML实体,并替换相应字符
decoded_data = html.unescape(data).replace('<p><br></p>', '\n').replace('<p>','').replace('</p>','')


# 输出结果
print(decoded_data)



2. 목표 2: 태그 내용 추출

아이디어: 사실은 정규 매칭입니다.

img 태그가 제거되고 래핑되어 URL만 남습니다.

암호:

import re

text = '<img src="URL">…………(此处省略一万字)'

# 提取URL
urls = re.findall(r'<img\s+src="([^"]+)"\s*>', text)

# 替换<img>标签为URL,并添加换行符
for url in urls:
    text = re.sub(r'<img\s+src="[^"]+"\s*>', url + '\n', text, count=1)

print(text)



3. 목표 3: 처리된 데이터를 원래 위치에 삽입

다음 코드의 사진 URL을 다운로드 받은 후 원래 위치에 맞게 문서에 삽입해주세요.

import requests
from docx import Document
from docx.shared import Inches

# 创建一个新的Word文档
doc = Document()

text = '''
图片:
https://xxxxx.png
'''

# 以换行符分割文本
lines = text.split('\n')

for line in lines:
    if line.startswith('https://'):
        # 下载图片
        response = requests.get(line)
        image_path = line.split('/')[-1]  # 使用URL中的最后一部分作为文件名保存图片
        with open(image_path, 'wb') as f:
            f.write(response.content)
        
        # 插入图片到Word文档
        doc.add_picture(image_path, width=Inches(4))  # 根据需要调整图片的宽度
    else:
        # 插入文本到Word文档
        doc.add_paragraph(line)

# 保存Word文档
doc.save("output.docx")

4. 목표 4: 지정된 위치에 지정된 콘텐츠를 삽입합니다.

Python을 사용하여 단어 문서를 열고 지정된 문자열 뒤에 내용을 씁니다.

from docx import Document

# 打开Word文档
doc = Document('example.docx')

# 获取文档中所有段落的内容
paragraphs = [p.text for p in doc.paragraphs]

# 指定要插入内容的位置
target_string = '指定字符串'
insert_index = paragraphs.index(target_string) + 1  # 在目标字符串后面插入,所以需要加1

# 要插入的内容
new_content = '要插入的内容'

# 在指定位置后插入内容
doc.paragraphs[insert_index].insert_paragraph_before(new_content)

# 保存修改后的Word文档
doc.save('example_modified.docx')



5. 목표 5: 상황에 맞는 글꼴 형식 지정

작성된 텍스트의 글꼴 크기는 이전 줄과 동일합니다.

from docx import Document
from docx.shared import Pt

# 打开Word文档
doc = Document('example.docx')

# 获取上一行的字体大小
previous_paragraph = doc.paragraphs[-1]
previous_run = previous_paragraph.runs[-1]
previous_font_size = previous_run.font.size

# 要写入的文本内容
new_text = '新的文本'

# 在新行中写入文本
new_paragraph = doc.add_paragraph()
new_run = new_paragraph.add_run(new_text)

# 设置新行的字体大小与上一行一致
new_font = new_run.font
new_font.size = previous_font_size

# 保存修改后的Word文档
doc.save('example_modified.docx')

이전 줄과 동일한 글꼴 크기의 텍스트를 삽입합니다.

from docx import Document
from docx.shared import Pt

def word_info_w():
    # 打开Word文档
    doc = Document('test.docx')

    # 获取文档中所有段落的内容
    paragraphs = [p.text for p in doc.paragraphs]

    # 指定要插入内容的位置
    target_string = '附件:'
    insert_index = paragraphs.index(target_string) + 1  # 在目标字符串后面插入,所以需要加1

    # 获取上一行的字体大小
    previous_paragraph = doc.paragraphs[insert_index - 1]
    previous_run = previous_paragraph.runs[-1]
    previous_font_size = previous_run.font.size

    # 要插入的内容
    new_content = '测试title'

    # 在指定位置后插入内容
    new_paragraph = doc.paragraphs[insert_index].insert_paragraph_before(new_content)

    # 设置新插入内容的字体大小与上一行一致
    new_run = new_paragraph.runs[0]
    new_font = new_run.font
    new_font.size = previous_font_size

    # 保存修改后的Word文档
    doc.save('test.docx')

if __name__ == '__main__':
    word_info_w()



6. 목표 6: 다양한 문자열을 여러 위치에 삽입

여러 다른 위치에 다른 문자열 삽입

(같은 위치에 삽입 가능)

from docx import Document

def insert_content(doc, insert_dict):
    # 获取文档中所有段落的内容
    paragraphs = [p.text for p in doc.paragraphs]

    for target_string, new_content in insert_dict.items():
        if target_string in paragraphs:
            # 指定要插入内容的位置
            insert_index = paragraphs.index(target_string) + 1  # 在目标字符串后面插入,所以需要加1

            # 获取上一行的字体大小
            previous_paragraph = doc.paragraphs[insert_index - 1]
            previous_run = previous_paragraph.runs[-1]
            previous_font_size = previous_run.font.size

            # 在指定位置后插入内容
            new_paragraph = doc.paragraphs[insert_index].insert_paragraph_before(new_content)

            # 设置新插入内容的字体大小与上一行一致
            new_run = new_paragraph.runs[0]
            new_font = new_run.font
            new_font.size = previous_font_size

    # 保存修改后的Word文档
    doc.save('test.docx')

if __name__ == '__main__':
    # 打开Word文档
    doc = Document('test.docx')

    # 定义要插入的内容和位置的字典
    insert_dict = {
        '附件:': '测试title1',
        '目录:': '测试title2'
    }

    # 插入内容
    insert_content(doc, insert_dict)



7. 목표 7: 다양한 문자열을 여러 위치에 삽입

from docx import Document

def insert_content(doc, target_string, new_content):
    # 获取文档中所有段落的内容
    paragraphs = [p.text for p in doc.paragraphs]

    if target_string in paragraphs:
        # 指定要插入内容的位置
        insert_index = paragraphs.index(target_string) + 1  # 在目标字符串后面插入,所以需要加1

        if insert_index < len(doc.paragraphs):
            # 在指定位置后插入内容
            doc.paragraphs[insert_index].insert_paragraph_before(new_content)

    # 保存修改后的 Word 文档
    doc.save('test.docx')

if __name__ == '__main__':
    # 打开 Word 文档
    doc = Document('test.docx')

    # 定义要插入的内容和位置的字典
    insert_dict = {
        '指定字符1位置': '插入内容1',
        '指定字符2位置': '插入内容2',
        '指定字符3位置': '插入内容3'
    }

    for target_string, new_content in insert_dict.items():
        # 插入内容
        insert_content(doc, target_string, new_content)

글꼴 크기 지정

from docx import Document
from docx.shared import Pt

def insert_content(doc, target_string, new_content):
    # 获取文档中所有段落的内容
    paragraphs = [p.text for p in doc.paragraphs]

    if target_string in paragraphs:
        # 指定要插入内容的位置
        insert_index = paragraphs.index(target_string) + 1  # 在目标字符串后面插入,所以需要加1

        if insert_index < len(doc.paragraphs):
            # 在指定位置后插入内容
            paragraph = doc.paragraphs[insert_index]
            run = paragraph.insert_paragraph_before(new_content).runs[0]
            font = run.font
            font.size = Pt(12)  # 设置字体大小为3号字体(12磅)

    # 保存修改后的 Word 文档
    doc.save('test.docx')

if __name__ == '__main__':
    # 打开 Word 文档
    doc = Document('test.docx')

    # 定义要插入的内容和位置的字典
    insert_dict = {
        '指定字符1位置': '插入内容1',
        '指定字符2位置': '插入内容2',
        '指定字符3位置': '插入内容3'
    }

    for target_string, new_content in insert_dict.items():
        # 插入内容
        insert_content(doc, target_string, new_content)



8. 목표 8: 지정된 문자열 뒤에 그래픽과 텍스트 쓰기

from docx import Document
from docx.shared import Pt
from docx.shared import Inches
import requests

def word_img_text_w(word, target_string):
    # 打开 Word 文档
    doc = Document('test.docx')

    # 获取文档中所有段落的内容
    paragraphs = [p.text for p in doc.paragraphs]

    if target_string in paragraphs:
        # 指定目标字符串的位置
        insert_index = paragraphs.index(target_string) + 1  # 在目标字符串后面插入,所以需要加1

        # 以换行符分割文本
        lines = word.split('\n')

        for line in lines:
            if line.startswith('https://'):
                # 下载图片
                response = requests.get(line)
                image_path = line.split('/')[-1]  # 图片保存的本地路径,使用URL中的最后一部分作为文件名
                with open(image_path, 'wb') as f:
                    f.write(response.content)
                # 插入图片到Word文档
                doc.paragraphs[insert_index].add_run().add_picture(image_path, width=Inches(4))  # 根据需要调整图片的宽度
                insert_index += 1
            else:
                # 插入文本到Word文档
                run = doc.paragraphs[insert_index].add_run(line)
                run.font.size = Pt(16)  # 设置字体大小为16磅
                insert_index += 1

    # 保存Word文档
    doc.save("test.docx")

if __name__ == '__main__':
    # 要插入的内容
    content = '''测试
https://xx.png
https://xxxx.png'''

    # 指定目标字符串
    target_string = '指定目标字符1'

    # 插入内容到Word文档
    word_img_text_w(content, target_string)

Supongo que te gusta

Origin blog.csdn.net/qq_53079406/article/details/132140855
Recomendado
Clasificación