目次
4. 目標 4: 指定されたコンテンツを指定された場所に挿入する
8. 目標 8: 指定された文字列の後ろにグラフィックとテキストを書き込む
1. 目標 1: デコード + ラベル解除
使用関数: html.unescape() デコード + replace() 置換
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 を使用して Word 文書を開き、指定された文字列の後にコンテンツを書き込みます
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)