первый коммит
This commit is contained in:
commit
683e32bee8
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
.idea
|
||||
gost-espd-generator.iml
|
||||
venv
|
||||
result-*
|
||||
|
13
README.md
Normal file
13
README.md
Normal file
@ -0,0 +1,13 @@
|
||||
# ГОСТ-ЕСПД генератор документов
|
||||
|
||||
## Генератор документа с исходным кодом
|
||||
|
||||
Запускать в той же директории, что и находится проект.
|
||||
|
||||
Использование:
|
||||
|
||||
```shell
|
||||
python3 source_code_generator.py /path/to/source/code /path/to/out.docx DOC.NUMBER
|
||||
```
|
||||
|
||||
После генерации нужно будет обновить ссылки в содержании и исправить текст в первых листах.
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
docxtpl
|
||||
|
44
source_code_generator.py
Normal file
44
source_code_generator.py
Normal file
@ -0,0 +1,44 @@
|
||||
from docxtpl import DocxTemplate
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
# Загрузка шаблона
|
||||
doc = DocxTemplate("template/source-code-template.docx")
|
||||
|
||||
|
||||
def get_sources(source_root):
|
||||
filenames = [str(path.absolute())[len(source_root):] for path in Path(source_root).rglob('*.*')]
|
||||
filenames.sort()
|
||||
return filenames
|
||||
|
||||
|
||||
def _get_file_context(source_root, filename):
|
||||
with open(source_root + filename, 'r', encoding="utf8") as f:
|
||||
return {
|
||||
'filename': filename.replace('\\', '/'), # для шиндовс, чтобы правильно генерить пути
|
||||
'content': f.read()
|
||||
}
|
||||
|
||||
|
||||
def render_source_code(source_root, out_filename, doc_number="РОФ.ГУТВ.00000-12"):
|
||||
# Данные для заполнения шаблона
|
||||
context = {
|
||||
'doc_number': doc_number,
|
||||
'files': [
|
||||
_get_file_context(source_root, src)
|
||||
for src in get_sources(source_root)
|
||||
]
|
||||
}
|
||||
|
||||
# Заполнение шаблона данными
|
||||
doc.render(context)
|
||||
|
||||
# Сохранение документа
|
||||
doc.save(out_filename)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) != 4:
|
||||
print(f"Usage: {sys.argv[0]} /path/to/source/code /path/to/out.docx DOC.NUMBER")
|
||||
else:
|
||||
render_source_code(sys.argv[1], sys.argv[2], doc_number=sys.argv[3])
|
BIN
template/source-code-template.docx
Normal file
BIN
template/source-code-template.docx
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user