py-docx-creator 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- py_docx_creator-0.1.0/PKG-INFO +72 -0
- py_docx_creator-0.1.0/README.md +61 -0
- py_docx_creator-0.1.0/py_docx_creator/AbstractClasses.py +292 -0
- py_docx_creator-0.1.0/py_docx_creator/CoreClasses.py +115 -0
- py_docx_creator-0.1.0/py_docx_creator/CustomClasses.py +51 -0
- py_docx_creator-0.1.0/py_docx_creator/__init__.py +45 -0
- py_docx_creator-0.1.0/py_docx_creator.egg-info/PKG-INFO +72 -0
- py_docx_creator-0.1.0/py_docx_creator.egg-info/SOURCES.txt +11 -0
- py_docx_creator-0.1.0/py_docx_creator.egg-info/dependency_links.txt +1 -0
- py_docx_creator-0.1.0/py_docx_creator.egg-info/requires.txt +1 -0
- py_docx_creator-0.1.0/py_docx_creator.egg-info/top_level.txt +1 -0
- py_docx_creator-0.1.0/pyproject.toml +20 -0
- py_docx_creator-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py_docx_creator
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Пакет для работы с .docx файлами
|
|
5
|
+
Author-email: Nikolay <kola8911764@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/HitsukaRgb/DocxPAPI/
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: python-docx
|
|
11
|
+
|
|
12
|
+
# DocxP
|
|
13
|
+
|
|
14
|
+
**DocxP** — это мой небольшой Python-проект для создания и форматирования Word-документов с использованием библиотеки `python-docx`.
|
|
15
|
+
Вы можете дополнять и расширять ее при необходимости. Я постарался построить гибкую основу для последующего расширения.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## Возможности
|
|
19
|
+
|
|
20
|
+
- Абстрактные классы для описания стилей документа, абзацев и текста.
|
|
21
|
+
- Реализация базовых стилей и логики генерации Word-документов.
|
|
22
|
+
- Гибкая настройка шрифтов, отступов, выравнивания и др.
|
|
23
|
+
|
|
24
|
+
## Структура проекта
|
|
25
|
+
|
|
26
|
+
- `core/` — модуль с основными и абстрактными классами:
|
|
27
|
+
- `AbstractClasses.py` — абстрактные интерфейсы
|
|
28
|
+
- `CoreClasses.py` — реализация базовых стилей
|
|
29
|
+
- `CustomClasses.py` — расширение с пользовательскими стилями
|
|
30
|
+
|
|
31
|
+
## Установка
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install python-docx py_docx_creator
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Пример использования
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from py_docx_creator.CoreClasses import CoreDocumentCreator
|
|
41
|
+
from py_docx_creator.CustomClasses import MainPageFormat, MainDocumentWriter, HeaderParagraphFormat, MainTextStyle, \
|
|
42
|
+
MainParagraphFormat
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class DocumentAPI(CoreDocumentCreator):
|
|
46
|
+
|
|
47
|
+
def __init__(self, file_name: str):
|
|
48
|
+
super().__init__()
|
|
49
|
+
self.file_name = file_name
|
|
50
|
+
|
|
51
|
+
def run(self):
|
|
52
|
+
self.create_document(self.file_name)
|
|
53
|
+
MainPageFormat().apply_style(document=self.document)
|
|
54
|
+
paragraph = MainDocumentWriter.add_paragraph_to_document(self.document)
|
|
55
|
+
HeaderParagraphFormat().apply_style(paragraph=paragraph)
|
|
56
|
+
run = MainDocumentWriter.add_run_to_paragraph(paragraph=paragraph, text="Какой либо текст")
|
|
57
|
+
MainTextStyle().apply_style(run=run)
|
|
58
|
+
|
|
59
|
+
paragraph = MainDocumentWriter.add_paragraph_to_document(self.document)
|
|
60
|
+
MainParagraphFormat().apply_style(paragraph=paragraph)
|
|
61
|
+
run = MainDocumentWriter.add_run_to_paragraph(paragraph=paragraph, text="Какой либо текст")
|
|
62
|
+
MainTextStyle().apply_style(run=run)
|
|
63
|
+
self.save_document()
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
if __name__ == '__main__':
|
|
67
|
+
|
|
68
|
+
DocumentAPI("Документ.docx").run()
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# DocxP
|
|
2
|
+
|
|
3
|
+
**DocxP** — это мой небольшой Python-проект для создания и форматирования Word-документов с использованием библиотеки `python-docx`.
|
|
4
|
+
Вы можете дополнять и расширять ее при необходимости. Я постарался построить гибкую основу для последующего расширения.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
## Возможности
|
|
8
|
+
|
|
9
|
+
- Абстрактные классы для описания стилей документа, абзацев и текста.
|
|
10
|
+
- Реализация базовых стилей и логики генерации Word-документов.
|
|
11
|
+
- Гибкая настройка шрифтов, отступов, выравнивания и др.
|
|
12
|
+
|
|
13
|
+
## Структура проекта
|
|
14
|
+
|
|
15
|
+
- `core/` — модуль с основными и абстрактными классами:
|
|
16
|
+
- `AbstractClasses.py` — абстрактные интерфейсы
|
|
17
|
+
- `CoreClasses.py` — реализация базовых стилей
|
|
18
|
+
- `CustomClasses.py` — расширение с пользовательскими стилями
|
|
19
|
+
|
|
20
|
+
## Установка
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install python-docx py_docx_creator
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Пример использования
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from py_docx_creator.CoreClasses import CoreDocumentCreator
|
|
30
|
+
from py_docx_creator.CustomClasses import MainPageFormat, MainDocumentWriter, HeaderParagraphFormat, MainTextStyle, \
|
|
31
|
+
MainParagraphFormat
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class DocumentAPI(CoreDocumentCreator):
|
|
35
|
+
|
|
36
|
+
def __init__(self, file_name: str):
|
|
37
|
+
super().__init__()
|
|
38
|
+
self.file_name = file_name
|
|
39
|
+
|
|
40
|
+
def run(self):
|
|
41
|
+
self.create_document(self.file_name)
|
|
42
|
+
MainPageFormat().apply_style(document=self.document)
|
|
43
|
+
paragraph = MainDocumentWriter.add_paragraph_to_document(self.document)
|
|
44
|
+
HeaderParagraphFormat().apply_style(paragraph=paragraph)
|
|
45
|
+
run = MainDocumentWriter.add_run_to_paragraph(paragraph=paragraph, text="Какой либо текст")
|
|
46
|
+
MainTextStyle().apply_style(run=run)
|
|
47
|
+
|
|
48
|
+
paragraph = MainDocumentWriter.add_paragraph_to_document(self.document)
|
|
49
|
+
MainParagraphFormat().apply_style(paragraph=paragraph)
|
|
50
|
+
run = MainDocumentWriter.add_run_to_paragraph(paragraph=paragraph, text="Какой либо текст")
|
|
51
|
+
MainTextStyle().apply_style(run=run)
|
|
52
|
+
self.save_document()
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
if __name__ == '__main__':
|
|
56
|
+
|
|
57
|
+
DocumentAPI("Документ.docx").run()
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
from docx.document import Document
|
|
4
|
+
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
|
5
|
+
from docx.shared import Pt, Inches
|
|
6
|
+
from docx.styles.styles import Styles
|
|
7
|
+
from docx.text.paragraph import Paragraph
|
|
8
|
+
from docx.text.run import Run
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class DocumentCreator(ABC):
|
|
12
|
+
"""Класс для создания, чтения, записи документа"""
|
|
13
|
+
|
|
14
|
+
def __init__(self):
|
|
15
|
+
self._file_name: str | None = None
|
|
16
|
+
self._path_to_document: str | None = None
|
|
17
|
+
self._document: Document | None = None
|
|
18
|
+
|
|
19
|
+
@abstractmethod
|
|
20
|
+
def create_document(self, file_name: str) -> None:
|
|
21
|
+
"""Создание документа"""
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
@abstractmethod
|
|
25
|
+
def load_document(self) -> None:
|
|
26
|
+
"""Загрузка уже имеющегося документа"""
|
|
27
|
+
pass
|
|
28
|
+
|
|
29
|
+
@abstractmethod
|
|
30
|
+
def save_document(self) -> None:
|
|
31
|
+
"""Сохранение документа"""
|
|
32
|
+
pass
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def file_name(self):
|
|
36
|
+
return self._file_name
|
|
37
|
+
|
|
38
|
+
@file_name.setter
|
|
39
|
+
def file_name(self, value):
|
|
40
|
+
self._file_name = value
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def path_to_document(self):
|
|
44
|
+
return self._path_to_document
|
|
45
|
+
|
|
46
|
+
@path_to_document.setter
|
|
47
|
+
def path_to_document(self, value):
|
|
48
|
+
self._path_to_document = value
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def document(self):
|
|
52
|
+
return self._document
|
|
53
|
+
|
|
54
|
+
@document.setter
|
|
55
|
+
def document(self, value):
|
|
56
|
+
self._document = value
|
|
57
|
+
|
|
58
|
+
class DocumentWriter(ABC):
|
|
59
|
+
"""Класс для наполнения документа"""
|
|
60
|
+
|
|
61
|
+
@staticmethod
|
|
62
|
+
@abstractmethod
|
|
63
|
+
def add_paragraph_to_document(document: Document) -> Paragraph | None:
|
|
64
|
+
"""Добавление параграфа в документ"""
|
|
65
|
+
pass
|
|
66
|
+
|
|
67
|
+
@staticmethod
|
|
68
|
+
@abstractmethod
|
|
69
|
+
def add_run_to_paragraph(paragraph: Paragraph, text: str) -> Run | None:
|
|
70
|
+
"""Добавить текст в параграф"""
|
|
71
|
+
pass
|
|
72
|
+
|
|
73
|
+
@staticmethod
|
|
74
|
+
@abstractmethod
|
|
75
|
+
def add_page_break(document: Document) -> None:
|
|
76
|
+
"""Добавление разрыва страницы в документ"""
|
|
77
|
+
pass
|
|
78
|
+
|
|
79
|
+
class DocumentStyle(ABC):
|
|
80
|
+
"""Стиль документа"""
|
|
81
|
+
|
|
82
|
+
def __init__(self):
|
|
83
|
+
self._document_style: str | None = None
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def document_style(self) -> str:
|
|
87
|
+
"""Стиль документа"""
|
|
88
|
+
return self._document_style
|
|
89
|
+
|
|
90
|
+
@document_style.setter
|
|
91
|
+
def document_style(self, value) -> None:
|
|
92
|
+
"""Стиль документа"""
|
|
93
|
+
self._document_style = value
|
|
94
|
+
|
|
95
|
+
@abstractmethod
|
|
96
|
+
def get_document_style(self, document: Document) -> Styles | None:
|
|
97
|
+
"""Получение стиля документа"""
|
|
98
|
+
pass
|
|
99
|
+
|
|
100
|
+
class PageStyle(ABC):
|
|
101
|
+
"""Отступы от краев страницы"""
|
|
102
|
+
|
|
103
|
+
def __init__(self):
|
|
104
|
+
self._top_margin: Pt | None = None
|
|
105
|
+
self._bottom_margin: Pt | None = None
|
|
106
|
+
self._left_margin: Pt | None = None
|
|
107
|
+
self._right_margin: Pt | None = None
|
|
108
|
+
|
|
109
|
+
@abstractmethod
|
|
110
|
+
def apply_style(self, document: Document) -> None:
|
|
111
|
+
"""Применение стиля"""
|
|
112
|
+
pass
|
|
113
|
+
|
|
114
|
+
@property
|
|
115
|
+
def top_margin(self) -> Pt:
|
|
116
|
+
"""Отступ сверху"""
|
|
117
|
+
return self._top_margin
|
|
118
|
+
|
|
119
|
+
@top_margin.setter
|
|
120
|
+
def top_margin(self, value: Pt) -> None:
|
|
121
|
+
"""Отступ сверху"""
|
|
122
|
+
self._top_margin = value
|
|
123
|
+
|
|
124
|
+
@property
|
|
125
|
+
def bottom_margin(self) -> Pt:
|
|
126
|
+
"""Отступ снизу"""
|
|
127
|
+
return self._bottom_margin
|
|
128
|
+
|
|
129
|
+
@bottom_margin.setter
|
|
130
|
+
def bottom_margin(self, value: Pt) -> None:
|
|
131
|
+
"""Отступ снизу"""
|
|
132
|
+
self._bottom_margin = value
|
|
133
|
+
|
|
134
|
+
@property
|
|
135
|
+
def left_margin(self) -> Pt:
|
|
136
|
+
"""Отступ слева"""
|
|
137
|
+
return self._left_margin
|
|
138
|
+
|
|
139
|
+
@left_margin.setter
|
|
140
|
+
def left_margin(self, value: Pt) -> None:
|
|
141
|
+
"""Отступ слева"""
|
|
142
|
+
self._left_margin = value
|
|
143
|
+
|
|
144
|
+
@property
|
|
145
|
+
def right_margin(self) -> Pt:
|
|
146
|
+
"""Отступ справа"""
|
|
147
|
+
return self._right_margin
|
|
148
|
+
|
|
149
|
+
@right_margin.setter
|
|
150
|
+
def right_margin(self, value: Pt) -> None:
|
|
151
|
+
"""Отступ справа"""
|
|
152
|
+
self._right_margin = value
|
|
153
|
+
|
|
154
|
+
class TextStyle(ABC):
|
|
155
|
+
"""Стиль текста"""
|
|
156
|
+
|
|
157
|
+
def __init__(self):
|
|
158
|
+
self._font_size: Pt | None = None
|
|
159
|
+
self._font_name: str | None = None
|
|
160
|
+
self._text_bold: bool | None = None
|
|
161
|
+
self._text_italic: bool | None = None
|
|
162
|
+
self._text_underline: bool | None = None
|
|
163
|
+
|
|
164
|
+
@abstractmethod
|
|
165
|
+
def apply_style(self, run: Run) -> None:
|
|
166
|
+
"""Применение стиля"""
|
|
167
|
+
pass
|
|
168
|
+
|
|
169
|
+
@property
|
|
170
|
+
def font_size(self) -> Pt:
|
|
171
|
+
"""Размер шрифта"""
|
|
172
|
+
return self._font_size
|
|
173
|
+
|
|
174
|
+
@font_size.setter
|
|
175
|
+
def font_size(self, value: Pt) -> None:
|
|
176
|
+
"""Размер шрифта"""
|
|
177
|
+
self._font_size = value
|
|
178
|
+
|
|
179
|
+
@property
|
|
180
|
+
def font_name(self) -> str:
|
|
181
|
+
"""Наименование шрифта"""
|
|
182
|
+
return self._font_name
|
|
183
|
+
|
|
184
|
+
@font_name.setter
|
|
185
|
+
def font_name(self, value: str) -> None:
|
|
186
|
+
"""Наименование шрифта"""
|
|
187
|
+
self._font_name = value
|
|
188
|
+
|
|
189
|
+
@property
|
|
190
|
+
def text_bold(self) -> bool:
|
|
191
|
+
"""Жирное начертание"""
|
|
192
|
+
return self._text_bold
|
|
193
|
+
|
|
194
|
+
@text_bold.setter
|
|
195
|
+
def text_bold(self, value: bool) -> None:
|
|
196
|
+
"""Жирное начертание"""
|
|
197
|
+
self._text_bold = value
|
|
198
|
+
|
|
199
|
+
@property
|
|
200
|
+
def text_italic(self) -> bool:
|
|
201
|
+
"""Курсивное начертание"""
|
|
202
|
+
return self._text_italic
|
|
203
|
+
|
|
204
|
+
@text_italic.setter
|
|
205
|
+
def text_italic(self, value: bool) -> None:
|
|
206
|
+
"""Курсивное начертание"""
|
|
207
|
+
self._text_italic = value
|
|
208
|
+
|
|
209
|
+
@property
|
|
210
|
+
def text_underline(self) -> bool:
|
|
211
|
+
"""Подчеркнутое начертание"""
|
|
212
|
+
return self._text_underline
|
|
213
|
+
|
|
214
|
+
@text_underline.setter
|
|
215
|
+
def text_underline(self, value: bool) -> None:
|
|
216
|
+
"""Подчеркнутое начертание"""
|
|
217
|
+
self._text_underline = value
|
|
218
|
+
|
|
219
|
+
class ParagraphStyle(ABC):
|
|
220
|
+
|
|
221
|
+
def __init__(self):
|
|
222
|
+
self._paragraph_alignment: WD_ALIGN_PARAGRAPH | None = None
|
|
223
|
+
self._space_after_paragraph: Pt | None = None
|
|
224
|
+
self._paragraph_left_indent: Inches | None = None
|
|
225
|
+
self._paragraph_right_indent: Inches | None = None
|
|
226
|
+
self._paragraph_line_spacing: float | None = None
|
|
227
|
+
self._paragraph_first_line_indent: Pt | None = None
|
|
228
|
+
|
|
229
|
+
@abstractmethod
|
|
230
|
+
def apply_style(self, paragraph: Paragraph) -> None:
|
|
231
|
+
"""Применение стиля к параграфу"""
|
|
232
|
+
pass
|
|
233
|
+
|
|
234
|
+
@property
|
|
235
|
+
def paragraph_alignment(self) -> WD_ALIGN_PARAGRAPH:
|
|
236
|
+
"""Выравнивание текста параграфа"""
|
|
237
|
+
return self._paragraph_alignment
|
|
238
|
+
|
|
239
|
+
@paragraph_alignment.setter
|
|
240
|
+
def paragraph_alignment(self, value: WD_ALIGN_PARAGRAPH) -> None:
|
|
241
|
+
"""Выравнивание текста параграфа"""
|
|
242
|
+
self._paragraph_alignment = value
|
|
243
|
+
|
|
244
|
+
@property
|
|
245
|
+
def space_after_paragraph(self) -> Pt:
|
|
246
|
+
"""Отступ после параграфа"""
|
|
247
|
+
return self._space_after_paragraph
|
|
248
|
+
|
|
249
|
+
@space_after_paragraph.setter
|
|
250
|
+
def space_after_paragraph(self, value: Pt) -> None:
|
|
251
|
+
"""Отступ после параграфа"""
|
|
252
|
+
self._space_after_paragraph = value
|
|
253
|
+
|
|
254
|
+
@property
|
|
255
|
+
def paragraph_left_indent(self) -> Inches:
|
|
256
|
+
"""Отступ слева от параграфа"""
|
|
257
|
+
return self._paragraph_left_indent
|
|
258
|
+
|
|
259
|
+
@paragraph_left_indent.setter
|
|
260
|
+
def paragraph_left_indent(self, value: Inches) -> None:
|
|
261
|
+
"""Отступ слева от параграфа"""
|
|
262
|
+
self._paragraph_left_indent = value
|
|
263
|
+
|
|
264
|
+
@property
|
|
265
|
+
def paragraph_right_indent(self) -> Inches:
|
|
266
|
+
"""Отступ справа от параграфа"""
|
|
267
|
+
return self._paragraph_right_indent
|
|
268
|
+
|
|
269
|
+
@paragraph_right_indent.setter
|
|
270
|
+
def paragraph_right_indent(self, value: Inches) -> None:
|
|
271
|
+
"""Отступ справа от параграфа"""
|
|
272
|
+
self._paragraph_right_indent = value
|
|
273
|
+
|
|
274
|
+
@property
|
|
275
|
+
def paragraph_line_spacing(self) -> float:
|
|
276
|
+
"""Межстрочный интервал параграфа"""
|
|
277
|
+
return self._paragraph_line_spacing
|
|
278
|
+
|
|
279
|
+
@paragraph_line_spacing.setter
|
|
280
|
+
def paragraph_line_spacing(self, value: float) -> None:
|
|
281
|
+
"""Межстрочный интервал параграфа"""
|
|
282
|
+
self._paragraph_line_spacing = value
|
|
283
|
+
|
|
284
|
+
@property
|
|
285
|
+
def paragraph_first_line_indent(self) -> Pt:
|
|
286
|
+
"""Отступ слева первой строки параграфа"""
|
|
287
|
+
return self._paragraph_first_line_indent
|
|
288
|
+
|
|
289
|
+
@paragraph_first_line_indent.setter
|
|
290
|
+
def paragraph_first_line_indent(self, value: Pt) -> None:
|
|
291
|
+
"""Отступ слева первой строки параграфа"""
|
|
292
|
+
self._paragraph_first_line_indent = value
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
|
|
3
|
+
from docx import Document
|
|
4
|
+
from docx.styles.styles import Styles
|
|
5
|
+
from docx.text.paragraph import Paragraph
|
|
6
|
+
from docx.text.run import Run
|
|
7
|
+
|
|
8
|
+
from py_docx_creator.AbstractClasses import DocumentCreator, DocumentWriter, DocumentStyle, PageStyle, ParagraphStyle, TextStyle
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FontNames(Enum):
|
|
12
|
+
"""Перечень наименований шрифтов"""
|
|
13
|
+
TimesNewRoman = "Times New Roman"
|
|
14
|
+
class DocumentStyles(Enum):
|
|
15
|
+
"""Перечень стилей документа"""
|
|
16
|
+
Normal = "Normal"
|
|
17
|
+
class CoreDocumentCreator(DocumentCreator):
|
|
18
|
+
|
|
19
|
+
def __init__(self):
|
|
20
|
+
super().__init__()
|
|
21
|
+
self._document = None
|
|
22
|
+
self._file_name = None
|
|
23
|
+
def create_document(self, file_name):
|
|
24
|
+
self.document = Document()
|
|
25
|
+
self.file_name = file_name
|
|
26
|
+
|
|
27
|
+
def load_document(self):
|
|
28
|
+
self.document = Document(self.path_to_document or self.file_name)
|
|
29
|
+
|
|
30
|
+
def save_document(self):
|
|
31
|
+
self.document.save(self.file_name)
|
|
32
|
+
class CoreDocumentWriter(DocumentWriter):
|
|
33
|
+
|
|
34
|
+
def __init__(self):
|
|
35
|
+
super().__init__()
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def add_paragraph_to_document(document: Document) -> Paragraph:
|
|
39
|
+
return document.add_paragraph()
|
|
40
|
+
|
|
41
|
+
@staticmethod
|
|
42
|
+
def add_run_to_paragraph(paragraph: Paragraph, text: str) -> Run:
|
|
43
|
+
return paragraph.add_run(text)
|
|
44
|
+
|
|
45
|
+
@staticmethod
|
|
46
|
+
def add_page_break(document: Document) -> None:
|
|
47
|
+
document.add_page_break()
|
|
48
|
+
class CoreDocumentStyle(DocumentStyle):
|
|
49
|
+
|
|
50
|
+
def __init__(self):
|
|
51
|
+
super().__init__()
|
|
52
|
+
|
|
53
|
+
def get_document_style(self, document: Document) -> Styles:
|
|
54
|
+
return document.style[f"{self.document_style}"]
|
|
55
|
+
class CorePageStyle(PageStyle):
|
|
56
|
+
"""Абстрактный класс реализации применения стиля к страницам документа"""
|
|
57
|
+
|
|
58
|
+
def __init__(self):
|
|
59
|
+
super().__init__()
|
|
60
|
+
|
|
61
|
+
def apply_style(self, document):
|
|
62
|
+
for section in document.sections:
|
|
63
|
+
section.top_margin = self.top_margin
|
|
64
|
+
section.bottom_margin = self.bottom_margin
|
|
65
|
+
section.left_margin = self.left_margin
|
|
66
|
+
section.right_margin = self.right_margin
|
|
67
|
+
class CoreParagraphFormat(ParagraphStyle):
|
|
68
|
+
""" Абстрактный класс реализации метода применения стиля к параграфу"""
|
|
69
|
+
|
|
70
|
+
def __init__(self):
|
|
71
|
+
super().__init__()
|
|
72
|
+
|
|
73
|
+
def apply_style(self, paragraph) -> None:
|
|
74
|
+
paragraph_style = paragraph.paragraph_format
|
|
75
|
+
|
|
76
|
+
if self.paragraph_alignment is not None:
|
|
77
|
+
paragraph_style.alignment = self.paragraph_alignment
|
|
78
|
+
|
|
79
|
+
if self.space_after_paragraph is not None:
|
|
80
|
+
paragraph_style.space_after = self.space_after_paragraph
|
|
81
|
+
|
|
82
|
+
if self.paragraph_left_indent is not None:
|
|
83
|
+
paragraph_style.left_indent = self.paragraph_left_indent
|
|
84
|
+
|
|
85
|
+
if self.paragraph_right_indent is not None:
|
|
86
|
+
paragraph_style.right_indent = self.paragraph_right_indent
|
|
87
|
+
|
|
88
|
+
if self.paragraph_line_spacing is not None:
|
|
89
|
+
paragraph_style.line_spacing = self.paragraph_line_spacing
|
|
90
|
+
|
|
91
|
+
if self.paragraph_first_line_indent is not None:
|
|
92
|
+
paragraph_style.first_line_indent = self.paragraph_first_line_indent
|
|
93
|
+
class CoreTextStyle(TextStyle):
|
|
94
|
+
"""Основной стиль текста"""
|
|
95
|
+
def __init__(self):
|
|
96
|
+
super().__init__()
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def apply_style(self, run):
|
|
100
|
+
if self.text_bold is not None:
|
|
101
|
+
run.font.bold = self.text_bold
|
|
102
|
+
if self.text_italic is not None:
|
|
103
|
+
run.font.italic = self.text_italic
|
|
104
|
+
if self.text_underline is not None:
|
|
105
|
+
run.font.underline = self.text_underline
|
|
106
|
+
if self.font_size is not None:
|
|
107
|
+
run.font.size = self.font_size
|
|
108
|
+
if self.font_name is not None:
|
|
109
|
+
run.font.name = self.font_name
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
|
2
|
+
from docx.shared import Pt, Inches
|
|
3
|
+
|
|
4
|
+
from py_docx_creator.CoreClasses import CoreDocumentStyle, DocumentStyles, CorePageStyle, CoreParagraphFormat, CoreTextStyle, \
|
|
5
|
+
FontNames, CoreDocumentWriter
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class NormalDocumentStyle(CoreDocumentStyle):
|
|
9
|
+
"""Стандартный стиль документа"""
|
|
10
|
+
def __init__(self):
|
|
11
|
+
super().__init__()
|
|
12
|
+
self.document_style = DocumentStyles.Normal.value
|
|
13
|
+
|
|
14
|
+
class MainPageFormat(CorePageStyle):
|
|
15
|
+
"""Основной формат страницы"""
|
|
16
|
+
def __init__(self):
|
|
17
|
+
super().__init__()
|
|
18
|
+
self.top_margin = Pt(15)
|
|
19
|
+
self.bottom_margin = Pt(10)
|
|
20
|
+
self.left_margin = Pt(75)
|
|
21
|
+
self.right_margin = Pt(75)
|
|
22
|
+
|
|
23
|
+
class MainParagraphFormat(CoreParagraphFormat):
|
|
24
|
+
"""Стиль основного текста"""
|
|
25
|
+
def __init__(self):
|
|
26
|
+
super().__init__()
|
|
27
|
+
self.paragraph_alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
|
|
28
|
+
self.space_after_paragraph = Pt(0)
|
|
29
|
+
self.paragraph_left_indent = Inches(-0.5)
|
|
30
|
+
self.paragraph_right_indent = Inches(-0.5)
|
|
31
|
+
self.paragraph_line_spacing = 1.15
|
|
32
|
+
self.paragraph_first_line_indent = 20
|
|
33
|
+
|
|
34
|
+
class HeaderParagraphFormat(CoreParagraphFormat):
|
|
35
|
+
"""Стиль для заголовков """
|
|
36
|
+
def __init__(self):
|
|
37
|
+
super().__init__()
|
|
38
|
+
self.paragraph_alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
39
|
+
self.paragraph_left_indent = Inches(-0.5)
|
|
40
|
+
self.paragraph_right_indent = Inches(-0.5)
|
|
41
|
+
|
|
42
|
+
class MainTextStyle(CoreTextStyle):
|
|
43
|
+
"""Основной стиль текста"""
|
|
44
|
+
def __init__(self):
|
|
45
|
+
super().__init__()
|
|
46
|
+
self.font_size = Pt(10)
|
|
47
|
+
self.font_name = FontNames.TimesNewRoman.value
|
|
48
|
+
|
|
49
|
+
class MainDocumentWriter(CoreDocumentWriter):
|
|
50
|
+
def __init__(self):
|
|
51
|
+
super().__init__()
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# default lib
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
# python-docx
|
|
6
|
+
from docx import Document
|
|
7
|
+
from docx.styles.styles import Styles
|
|
8
|
+
from docx.text.paragraph import Paragraph
|
|
9
|
+
from docx.text.run import Run
|
|
10
|
+
from docx.shared import Pt, Inches
|
|
11
|
+
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
|
12
|
+
|
|
13
|
+
from py_docx_creator.CoreClasses import (
|
|
14
|
+
CoreDocumentWriter,
|
|
15
|
+
CorePageStyle,
|
|
16
|
+
CoreParagraphFormat,
|
|
17
|
+
CoreTextStyle,
|
|
18
|
+
DocumentStyles,
|
|
19
|
+
FontNames,
|
|
20
|
+
CoreDocumentStyle,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
from py_docx_creator.AbstractClasses import (DocumentStyle,
|
|
24
|
+
PageStyle,
|
|
25
|
+
TextStyle,
|
|
26
|
+
ParagraphStyle,
|
|
27
|
+
DocumentCreator,
|
|
28
|
+
DocumentWriter)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
|
|
33
|
+
# default lib
|
|
34
|
+
'Enum', 'ABC', 'abstractmethod',
|
|
35
|
+
|
|
36
|
+
#python-docx
|
|
37
|
+
'Document', 'Styles', 'Paragraph', 'Run', 'Pt', 'Inches', 'WD_ALIGN_PARAGRAPH',
|
|
38
|
+
|
|
39
|
+
#AbstractClasses
|
|
40
|
+
"DocumentStyle", "PageStyle", "TextStyle", "ParagraphStyle", "DocumentCreator", "DocumentWriter",
|
|
41
|
+
|
|
42
|
+
#CoreClasses
|
|
43
|
+
'CoreDocumentWriter', 'CorePageStyle', 'CoreParagraphFormat', 'CoreTextStyle', 'DocumentStyles', 'FontNames',
|
|
44
|
+
'CoreDocumentStyle',
|
|
45
|
+
]
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py_docx_creator
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Пакет для работы с .docx файлами
|
|
5
|
+
Author-email: Nikolay <kola8911764@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/HitsukaRgb/DocxPAPI/
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: python-docx
|
|
11
|
+
|
|
12
|
+
# DocxP
|
|
13
|
+
|
|
14
|
+
**DocxP** — это мой небольшой Python-проект для создания и форматирования Word-документов с использованием библиотеки `python-docx`.
|
|
15
|
+
Вы можете дополнять и расширять ее при необходимости. Я постарался построить гибкую основу для последующего расширения.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## Возможности
|
|
19
|
+
|
|
20
|
+
- Абстрактные классы для описания стилей документа, абзацев и текста.
|
|
21
|
+
- Реализация базовых стилей и логики генерации Word-документов.
|
|
22
|
+
- Гибкая настройка шрифтов, отступов, выравнивания и др.
|
|
23
|
+
|
|
24
|
+
## Структура проекта
|
|
25
|
+
|
|
26
|
+
- `core/` — модуль с основными и абстрактными классами:
|
|
27
|
+
- `AbstractClasses.py` — абстрактные интерфейсы
|
|
28
|
+
- `CoreClasses.py` — реализация базовых стилей
|
|
29
|
+
- `CustomClasses.py` — расширение с пользовательскими стилями
|
|
30
|
+
|
|
31
|
+
## Установка
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install python-docx py_docx_creator
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Пример использования
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from py_docx_creator.CoreClasses import CoreDocumentCreator
|
|
41
|
+
from py_docx_creator.CustomClasses import MainPageFormat, MainDocumentWriter, HeaderParagraphFormat, MainTextStyle, \
|
|
42
|
+
MainParagraphFormat
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class DocumentAPI(CoreDocumentCreator):
|
|
46
|
+
|
|
47
|
+
def __init__(self, file_name: str):
|
|
48
|
+
super().__init__()
|
|
49
|
+
self.file_name = file_name
|
|
50
|
+
|
|
51
|
+
def run(self):
|
|
52
|
+
self.create_document(self.file_name)
|
|
53
|
+
MainPageFormat().apply_style(document=self.document)
|
|
54
|
+
paragraph = MainDocumentWriter.add_paragraph_to_document(self.document)
|
|
55
|
+
HeaderParagraphFormat().apply_style(paragraph=paragraph)
|
|
56
|
+
run = MainDocumentWriter.add_run_to_paragraph(paragraph=paragraph, text="Какой либо текст")
|
|
57
|
+
MainTextStyle().apply_style(run=run)
|
|
58
|
+
|
|
59
|
+
paragraph = MainDocumentWriter.add_paragraph_to_document(self.document)
|
|
60
|
+
MainParagraphFormat().apply_style(paragraph=paragraph)
|
|
61
|
+
run = MainDocumentWriter.add_run_to_paragraph(paragraph=paragraph, text="Какой либо текст")
|
|
62
|
+
MainTextStyle().apply_style(run=run)
|
|
63
|
+
self.save_document()
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
if __name__ == '__main__':
|
|
67
|
+
|
|
68
|
+
DocumentAPI("Документ.docx").run()
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
py_docx_creator/AbstractClasses.py
|
|
4
|
+
py_docx_creator/CoreClasses.py
|
|
5
|
+
py_docx_creator/CustomClasses.py
|
|
6
|
+
py_docx_creator/__init__.py
|
|
7
|
+
py_docx_creator.egg-info/PKG-INFO
|
|
8
|
+
py_docx_creator.egg-info/SOURCES.txt
|
|
9
|
+
py_docx_creator.egg-info/dependency_links.txt
|
|
10
|
+
py_docx_creator.egg-info/requires.txt
|
|
11
|
+
py_docx_creator.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
python-docx
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
py_docx_creator
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "py_docx_creator"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Пакет для работы с .docx файлами"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {text = "MIT"}
|
|
11
|
+
authors = [
|
|
12
|
+
{name = "Nikolay", email = "kola8911764@gmail.com"}
|
|
13
|
+
]
|
|
14
|
+
requires-python = ">=3.12"
|
|
15
|
+
dependencies = [
|
|
16
|
+
"python-docx"
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
"Homepage" = "https://github.com/HitsukaRgb/DocxPAPI/"
|