Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from docx import Document
- code = "ыпврп лпыаы ыпдло лдвыд оапла дыада лпврд врпвы лооыв ваылв ралдд ыород роыра влпао дапоа алвдл рлвра рыппр длрпд пдпаа оплоа рылыы ыврпа пдвлл длвдд плваы плвды оыпда рорлд адыаы"
- from docx.enum.style import WD_STYLE_TYPE
- from docx.shared import Inches, Pt
- #https://python-docx.readthedocs.io/en/latest/api/text.html#docx.text.parfmt.ParagraphFormat
- def make_ex(document, ex_text, ex_title, style):
- text_p = document.add_paragraph(style=change_format(style, 'ex'))
- class doc_maker():
- document = None
- style = None
- paragraphs = None
- def _change_format(self, paragraph, format):
- if format == 'ex':
- paragraph_format = paragraph.paragraph_format
- paragraph_format.left_indent = Inches(0.25)
- paragraph_format.space_before = Pt(15)
- paragraph_format.line_spacing = 1.5
- paragraph_format.widow_control = True
- paragraph_format.space_after = 2
- if format == 'ex name':
- paragraph_format = style.paragraph_format
- paragraph_format.left_indent = Inches(0.25)
- paragraph_format.space_before = Pt(12)
- paragraph_format.widow_control = True
- return paragraph_format
- def __init__(self):
- self.document = Document()
- self.style = self.document.styles.add_style('Indent', WD_STYLE_TYPE.PARAGRAPH)
- self.paragraphs = []
- def save(self, path:str):
- self.document.save('demo.docx')
- def write_ex_text(self, text):
- p = self.document.add_paragraph()
- #Position
- paragraph_format = p.paragraph_format
- paragraph_format.left_indent = Inches(0.25)
- paragraph_format.space_before = Pt(15)
- paragraph_format.line_spacing = 1.5
- paragraph_format.widow_control = True
- paragraph_format.space_after = 2
- #Style
- run = p.add_run(code)
- run.bold = True
- run.font.size = Pt(15)
- self.paragraphs.append(run)
- return self
- #document.add_page_break()
- doc_maker().write_ex_text(code).write_ex_text(code).save('demo.docx')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement