Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import docx
- def run_get_style(run) -> str:
- """Returns the font style of the run.
- Does not consider a paragraph style that makes the whole Paragraph bold or italic.
- Args:
- run: A docx.Run object.
- Returns:
- A string indicating the font style of the run: "bold", "italic", or "normal".
- """
- if run.bold:
- return "bold"
- elif run.italic:
- return "italic"
- else:
- return "normal"
- def detect_fonts(document: docx.Document) -> None:
- """Detects the font styles in the document and writes them to a HTML file.
- Args:
- document: A docx.Document object.
- """
- with open("bebe.html", "w") as f:
- current_style = None
- current_text = ""
- for paragraph in document.paragraphs:
- if paragraph.runs:
- run_style = run_get_style(paragraph.runs[0])
- if current_style != run_style:
- if current_text:
- if current_style == "bold":
- f.write(f"<p><b>{current_text}</b></p>\n")
- elif current_style == "italic":
- f.write(f"<p><em>{current_text}</em></p>\n")
- else:
- f.write(f"<p>{current_text}</p>\n")
- current_style = run_style
- current_text = paragraph.text
- elif paragraph.text == "Bebe este așa":
- f.write(f"<p><b>{paragraph.text}</b></p>\n")
- elif paragraph.text:
- current_text += paragraph.text
- if current_text:
- if current_style == "bold":
- f.write(f"<p><b>{current_text}</b></p>\n")
- elif current_style == "italic":
- f.write(f"<p><em>{current_text}</em></p>\n")
- else:
- f.write(f"<p>{current_text}</p>\n")
- def main():
- document = docx.Document("bebe.docx")
- detect_fonts(document)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement