pqzx / html2docx

Convert html to docx
MIT License
69 stars 49 forks source link

Handle missing run for leading br tag #53

Open dashingdove opened 1 year ago

dashingdove commented 1 year ago

(Issue #37) When the HTML to parse begins with a <br> tag, an error is thrown because self.run is not defined. This is a simple fix to handle the error.

dashingdove commented 1 year ago

Can you use this to recreate a real line break?

try:
    self.run.add_break()
except AttributeError: #If <br> is the first element parsed, self.run will not be defined
    self.paragraph = self.doc.add_paragraph()
    self.run = self.paragraph.add_run()
    self.run.add_break()

Yes, you could add a line break in that way. However, adding the paragraph itself creates a new line and so you would end up with the appearance of two new lines, I believe.