简体   繁体   English

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

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

I am using win32com to convert html to word in python (Django). 我正在使用win32com将html转换为python(Django)中的word。

Am facing issues with images part ie the html page contains images which are not coming in the final word doc. 图像部分面临问题,即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()

This is the code am using. 这是代码正在使用。 What can be done about this ? 关于这个还能做什么 ?

Unfortunately, this appears to be a shortcoming of Word. 不幸的是,这似乎是Word的缺点。 Refer to here for more information. 有关更多信息,请参考此处

The 'simplest' solution is to open the html document, select all, copy and then paste into a new document. “最简单”的解决方案是打开html文档,全选,复制,然后粘贴到新文档中。 This will embed the images. 这将嵌入图像。

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