Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import docx
- def run_get_style(run) -> str:
- if run.bold:
- return "bold"
- elif run.italic:
- return "italic"
- else:
- return "normal"
- def convert_docx_to_html_with_styles(document: docx.Document, output_file: str) -> None:
- with open(output_file, "w") as f:
- f.write("<html>\n<body>\n")
- current_style = None
- for paragraph in document.paragraphs:
- runs = paragraph.runs
- if not runs:
- continue
- for run in runs:
- run_style = run_get_style(run)
- if run_style == current_style:
- f.write(run.text)
- else:
- if current_style:
- if current_style == "bold":
- f.write("</b>")
- elif current_style == "italic":
- f.write("</em>")
- if run_style == "bold":
- f.write("<b>")
- elif run_style == "italic":
- f.write("<em>")
- f.write(run.text)
- current_style = run_style
- f.write("<br>\n") # Adăugăm un salt de linie între paragrafe
- if current_style:
- if current_style == "bold":
- f.write("</b>")
- elif current_style == "italic":
- f.write("</em>")
- f.write("</body>\n</html>")
- def main():
- document = docx.Document("bebe.docx")
- convert_docx_to_html_with_styles(document, "bebe.html")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement