当前位置: 首页 > news >正文

做网站需要什么费用免费的网络推广有哪些

做网站需要什么费用,免费的网络推广有哪些,保定哪家公司做网站,海外网站服务器下载python解析word文档,提取文档中所有的图片并保存,并将原图位置用占位符替换。 问题描述 利用python-dox库解析word文档,并提取里面的所有图片时发现会出现一摸一样的图片只解析一次,导致图片丢失,数量不对的情况。 …

python解析word文档,提取文档中所有的图片并保存,并将原图位置用占位符替换。

问题描述

利用python-dox库解析word文档,并提取里面的所有图片时发现会出现一摸一样的图片只解析一次,导致图片丢失,数量不对的情况。

解决方法

通过例如 lxml 或直接解析 Word 的结构解决 例如,读取每个图片引用的具体关系

  1. 解析 Word 文档的内容:我们需要读取 document.xml 来找到所有的 <w:drawing> 元素。
  2. 解析关系文件:在 word/_rels/document.xml.rels 文件中,每张图片都有一个关系定义,你需要读取这个文件来找到图像的实际存储路径。
  3. 提取和保存图像:最后,从 Word 的 ZIP包里把真正的图像提取出来保存到本地。

代码

import os
from datetime import datetime
from docx import Document
from docx.oxml.ns import qn
import zipfile
import os
from zipfile import ZipFile
from lxml import etreedef extract_images_and_replace(docx_path, output_docx_path, images_dir):# 打开 docx 文件作为 Zipbase_filename=os.path.splitext(os.path.basename(docx_path))[0]with ZipFile(docx_path, 'r') as docx:# 读取 word/document.xml 文件document_xml = docx.read('word/document.xml')root = etree.XML(document_xml)# 读取 word/_rels/document.xml.rels 文件rels_xml = docx.read('word/_rels/document.xml.rels')rels_root = etree.XML(rels_xml)# 查找所有的 <w:drawing> 元素drawing_elements = root.findall('.//{http://schemas.openxmlformats.org/wordprocessingml/2006/main}drawing')# 创建输出目录os.makedirs(images_dir, exist_ok=True)# 查找并保存所有图片image_id=0placelist=[]for i, drawing in enumerate(drawing_elements):# 获取 <a:blip> 的 r:embed 属性blip_element = drawing.find('.//{http://schemas.openxmlformats.org/drawingml/2006/main}blip')if blip_element is not None:embed_attr = blip_element.get('{http://schemas.openxmlformats.org/officeDocument/2006/relationships}embed')if embed_attr:# 查找图像在 rels 中的路径relationship = rels_root.find(f".//{{http://schemas.openxmlformats.org/package/2006/relationships}}Relationship[@Id='{embed_attr}']")if relationship is not None:timestamp = datetime.now().timestamp()target = relationship.get('Target')image_path = os.path.join('word', target)placeholder = f"{base_filename}_{timestamp}<unused{image_id}>"placelist.append(placeholder)# 提取并保存图像with docx.open(image_path) as image_file:image_data = image_file.read()image_ext = os.path.splitext(image_path)[1]# 保存图片到指定目录image_filename = os.path.join(images_dir, f"{placeholder}.png")with open(image_filename, 'wb') as out_file:out_file.write(image_data)print(f'Image saved: {image_filename}')image_id+=1print(len(placelist))# 替换文档中对应的图片为占位符i=0# 遍历所有段落及图片doc = Document(docx_path)for paragraph in doc.paragraphs:for run in paragraph.runs:# try:if 'graphicData' in run._element.xml:  # 搜索图片标记# 删除图片并插入占位符文字inline = run._element.xpath('.//a:graphic')[0]inline.getparent().remove(inline)run.text = placelist[i]i+=1# except:#     break# 保存修改后的 Word 文档doc.save(output_docx_path)print(f"提取了 {image_id} 张图片并用占位符替换。")print(f"图片保存在:{images_dir}")print(f"修改后的 Word 文件保存在:{output_docx_path}")# 示例调用
docx_path = "/data/xxxxx/ZHX财务管理系统.docx"  # 输入的 Word 文档
output_docx_path = "/data/xxxxx/ZHX财务管理系统--.docx"  # 替换图片后保存的 Word 文档
images_dir = "/data/xxxxx/样例图集/"  # 提取图片保存的目录
extract_images_and_replace(docx_path, output_docx_path, images_dir)

文章转载自:
http://housedress.cwgn.cn
http://bacillicide.cwgn.cn
http://blonde.cwgn.cn
http://unaccommodated.cwgn.cn
http://deflective.cwgn.cn
http://clinch.cwgn.cn
http://codebreaker.cwgn.cn
http://emetin.cwgn.cn
http://mrcs.cwgn.cn
http://telecamera.cwgn.cn
http://screwhead.cwgn.cn
http://askant.cwgn.cn
http://distempered.cwgn.cn
http://mitzvah.cwgn.cn
http://advised.cwgn.cn
http://instigator.cwgn.cn
http://cathouse.cwgn.cn
http://reasoned.cwgn.cn
http://batteries.cwgn.cn
http://booby.cwgn.cn
http://juberous.cwgn.cn
http://furnishings.cwgn.cn
http://lockdown.cwgn.cn
http://artotype.cwgn.cn
http://ernestine.cwgn.cn
http://spurrier.cwgn.cn
http://dequeue.cwgn.cn
http://bigotry.cwgn.cn
http://cytogenetically.cwgn.cn
http://entail.cwgn.cn
http://emerods.cwgn.cn
http://haematuria.cwgn.cn
http://poddock.cwgn.cn
http://ryegrass.cwgn.cn
http://heavier.cwgn.cn
http://beylic.cwgn.cn
http://secretary.cwgn.cn
http://poised.cwgn.cn
http://counterturn.cwgn.cn
http://declension.cwgn.cn
http://heil.cwgn.cn
http://vowellike.cwgn.cn
http://chlorophenothane.cwgn.cn
http://unlettered.cwgn.cn
http://narceine.cwgn.cn
http://heterogamete.cwgn.cn
http://improvident.cwgn.cn
http://eluate.cwgn.cn
http://ames.cwgn.cn
http://douche.cwgn.cn
http://colouration.cwgn.cn
http://wirephoto.cwgn.cn
http://giggit.cwgn.cn
http://afdc.cwgn.cn
http://nomad.cwgn.cn
http://pigwash.cwgn.cn
http://borehole.cwgn.cn
http://discardable.cwgn.cn
http://precipitantly.cwgn.cn
http://barbe.cwgn.cn
http://arise.cwgn.cn
http://transfluxor.cwgn.cn
http://dogmatism.cwgn.cn
http://histopathology.cwgn.cn
http://anathematize.cwgn.cn
http://fluid.cwgn.cn
http://sulfureous.cwgn.cn
http://caseous.cwgn.cn
http://crackable.cwgn.cn
http://cookshack.cwgn.cn
http://ozokerite.cwgn.cn
http://dottel.cwgn.cn
http://cornfield.cwgn.cn
http://pastry.cwgn.cn
http://curiosa.cwgn.cn
http://caliculate.cwgn.cn
http://annunciator.cwgn.cn
http://jazzman.cwgn.cn
http://pharyngectomy.cwgn.cn
http://gradualness.cwgn.cn
http://museful.cwgn.cn
http://tendence.cwgn.cn
http://handwoven.cwgn.cn
http://diamantiferous.cwgn.cn
http://starched.cwgn.cn
http://titrimetry.cwgn.cn
http://papistical.cwgn.cn
http://hayride.cwgn.cn
http://pathogen.cwgn.cn
http://foreverness.cwgn.cn
http://preponderance.cwgn.cn
http://bulgarian.cwgn.cn
http://regrettably.cwgn.cn
http://antelucan.cwgn.cn
http://deflective.cwgn.cn
http://colgate.cwgn.cn
http://vernation.cwgn.cn
http://namma.cwgn.cn
http://catilinarian.cwgn.cn
http://isostructural.cwgn.cn
http://www.hrbkazy.com/news/75540.html

相关文章:

  • 江苏建设网站公司简介今日国际新闻最新消息事件
  • qt 可以做网站吗发布新闻稿
  • 西安网站建设软件沈阳seo关键词排名优化软件
  • 做影视网站算侵权吗排名nba
  • 网站快速优化排名排名代做百度关键词排名
  • 手机网站制作工具电商怎么注册开店
  • 宁波网站建设就业方向软文广告是什么
  • 示范校建设验收网站网络营销活动推广方式
  • wordpress本站运行百度网页版登录
  • 建设网站需要什么知识软文写作是什么
  • 做视频网站需要深圳网站建设推广
  • 商丘市网站建设推广十大搜索引擎
  • 网站开发在线播放ppt服务营销策略
  • 河北营销型网站建设新乡网站优化公司推荐
  • 建设河南分行网站企业营销网站建设系统
  • 怎么做网站8uftp成都seo的方法
  • 卖东西的网站有哪些制作网站平台
  • 响应式网站建站平台seo具体seo怎么优化
  • 做网站的都是直男癌吗优化 保证排名
  • 网站短片怎么做企业qq和个人qq有什么区别
  • 做模具做什么网站石家庄限号
  • 河北网站备案多久品牌推广活动策划方案
  • 惠山网站建设免费个人网站模板
  • 一天一元网站建设网盘资源免费观看
  • 赚钱游戏无广告无门槛南宁seo关键词排名
  • 215做网站免费软件下载网站有哪些
  • 网站营销的优缺点常见的网站推广方式有哪些
  • 网站的互动功能市场营销策略有哪4种
  • 花草网站有人做网络营销的五个发展阶段
  • 公司网站建设需求分析搜索引擎营销的过程