繁体   English   中英

如何在python中使用win32com将带有图像的html转换为word

[英]How to convert html with images to word using win32com in python

我正在使用win32com将html转换为python(Django)中的word。

图像部分面临问题,即html页面包含最终单词doc中未包含的图像。

import win32com.client

word = win32com.client.Dispatch('Word.Application')

doc = word.Documents.Add('example.html')
doc.SaveAs('example.doc', FileFormat=0)
doc.Close()

word.Quit()

这是代码正在使用。 关于这个还能做什么 ?

不幸的是,这似乎是Word的缺点。 有关更多信息,请参考此处

“最简单”的解决方案是打开html文档,全选,复制,然后粘贴到新文档中。 这将嵌入图像。

import os
import win32com.client

word = win32com.client.Dispatch("Word.Application")

in_file  = os.path.abspath("example.html")
in_name  = os.path.splitext(os.path.split(in_file)[1])[0]
out_file = os.path.abspath("%s.doc" % in_name)

# Open and copy HTML
doc = word.Documents.Add(in_file)
word.Selection.WholeStory()
word.Selection.Copy()
doc.Close()

# Open new document, paste HTML and save
doc = word.Documents.Add()
word.Selection.Paste()
doc.SaveAs(out_file, FileFormat=0)
doc.Close()

word.Quit()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM