Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/html2docx.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 22 additions & 3 deletions htmldocx/h2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,11 +601,30 @@ def run_process(self, html):

def add_html_to_document(self, html, document):
if not isinstance(html, str):
raise ValueError('First argument needs to be a %s' % str)
raise ValueError('First argument needs to be a string')
elif not isinstance(document, docx.document.Document) and not isinstance(document, docx.table._Cell):
raise ValueError('Second argument needs to be a %s' % docx.document.Document)
raise ValueError('Second argument needs to be a Document or a Cell object')

self.set_initial_attrs(document)
self.run_process(html)
soup = BeautifulSoup(html, 'html.parser')
self.paragraph = document.add_paragraph()
for element in soup.descendants:
if isinstance(element, str):
self.run = self.paragraph.add_run(element.strip())
elif element.name == 'img':

img_path = element.get('src')
width = element.get('width', 100)
height = element.get('height', 50)
self.run = self.paragraph.add_run()
self.run.add_picture(img_path, width=Inches(float(width) / 100), height=Inches(float(height) / 100))
else:
continue


paragraph_format = self.paragraph.paragraph_format
paragraph_format.line_spacing = 1.0
paragraph_format.keep_with_next = True

def add_html_to_cell(self, html, cell):
if not isinstance(cell, docx.table._Cell):
Expand Down
12 changes: 12 additions & 0 deletions mytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from docx import Document
from htmldocx import HtmlToDocx

document = Document()
new_parser = HtmlToDocx()

html = '''
<p>there are some words:<img src="testimg.png"D:/kaiyuan/html2docx/testimg.png width="100" height="50" alt="测试图片"/>,there are some words in the same line。</p>
'''

new_parser.add_html_to_document(html, document)
document.save('test_output.docx')