首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用pypandoc将图像从docx文件添加到html文件

无法使用pypandoc将图像从docx文件添加到html文件
EN

Stack Overflow用户
提问于 2021-10-12 06:24:14
回答 2查看 62关注 0票数 0

我正在尝试使用python中的pypandoc包将docx文件转换为html文件。这是我的代码(删除了文件路径)-

代码语言:javascript
复制
import pypandoc
filename = <filepath>
output=pypandoc.convert(filename,to='html',extra_args=['--extract-media=<foldername>'])
filename=os.path.splitext(filename)[0]
filename="{0}.html".format(filename)
with open(filename,'w') as f:
  if type(output) is not str: 
     output=output.encode("utf-8")
  f.write(output)

它不会插入docx文件中存在的图像,文本的颜色将全部更改为黑白。我应该怎么做才能把所有的图片都放在html文件中,并保持所有的文本格式不变?

EN

回答 2

Stack Overflow用户

发布于 2021-10-12 06:45:14

也许你可以试试docx2html。代码如下:

代码语言:javascript
复制
import os.path
from shutil import copyfile

from docx2html import convert

def handle_image(image_id, relationship_dict):
    image_path = relationship_dict[image_id]
    # Now do something to the image. Let's move it somewhere.
    _, filename = os.path.split(image_path)
    destination_path = os.path.join('/tmp', filename)
    copyfile(image_path, destination_path)

    # Return the `src` attribute to be used in the img tag
    return 'file://%s' % destination

html = convert('path/to/docx/file', image_handler=handle_image)
票数 0
EN

Stack Overflow用户

发布于 2021-10-19 01:05:58

也许你可以试一下这个组件,如下所示。

代码语言:javascript
复制
pip install mammoth
代码语言:javascript
复制
import mammoth

with open("document.docx", "rb") as docx_file:
    result = mammoth.convert_to_html(docx_file)
    html = result.value # The generated HTML
    messages = result.messages # Any messages, such as warnings during conversion
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69535957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档