目录
使用函数:html.unescape()解码+replace()替换
- import html
-
- data = '\u003cp\u003e(此处忽略一万个字)'
-
- # 解码HTML实体,并替换相应字符
- decoded_data = html.unescape(data).replace('
', '\n').replace(''
,'').replace('','') -
-
- # 输出结果
- print(decoded_data)

思路:其实也就是正则匹配
img标签去掉并换行,只留下URL
代码:
- import re
-
- text = '
…………(此处省略一万字)' -
- # 提取URL
- urls = re.findall(r'
', text) -
- # 替换
标签为URL,并添加换行符 - for url in urls:
- text = re.sub(r'
', url + '\n', text, count=1) -
- print(text)
将以下代码中图片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")
使用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')
将写入文本的的字体大小与上一行一致
- 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()
向多个不同位置插入不同的字符串
(可能会插入到同一个位置)
- 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)
- 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)
- 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)