Advertisement
nicuf

bebe bun

Oct 3rd, 2023
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import docx
  2.  
  3. def run_get_style(run) -> str:
  4. if run.bold:
  5. return "bold"
  6. elif run.italic:
  7. return "italic"
  8. else:
  9. return "normal"
  10.  
  11. def convert_docx_to_html_with_styles(document: docx.Document, output_file: str) -> None:
  12. with open(output_file, "w") as f:
  13. f.write("<html>\n<body>\n")
  14. current_style = None
  15.  
  16. for paragraph in document.paragraphs:
  17. runs = paragraph.runs
  18. if not runs:
  19. continue
  20.  
  21. for run in runs:
  22. run_style = run_get_style(run)
  23. print(f"Run text: {run.text}, Style: {run_style}")
  24.  
  25. if run_style == current_style:
  26. f.write(run.text)
  27. else:
  28. if current_style:
  29. if current_style == "bold":
  30. f.write("</b>")
  31. if current_style == "italic":
  32. f.write("</em>")
  33. if run_style == "bold":
  34. f.write("<b>")
  35. if run_style == "italic":
  36. f.write("<em>")
  37.  
  38. f.write(run.text)
  39. current_style = run_style
  40.  
  41. if current_style:
  42. if current_style == "bold":
  43. f.write("</b>")
  44. if current_style == "italic":
  45. f.write("</em>")
  46. current_style = None
  47.  
  48. f.write("<br>\n") # Adăugăm un salt de linie între paragrafe
  49.  
  50. f.write("</body>\n</html>")
  51.  
  52.  
  53. def main():
  54. document = docx.Document("bebe.docx")
  55. convert_docx_to_html_with_styles(document, "bebe.html")
  56.  
  57. if __name__ == "__main__":
  58. main()
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement