Skip to content

Commit f9dddaf

Browse files
committed
created_module
1 parent 848c049 commit f9dddaf

12 files changed

Lines changed: 682 additions & 415 deletions

.gitignore

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# Test Input/Output
2+
*.in
3+
*.out
4+
5+
# Byte-compiled / optimized / DLL files
6+
__pycache__/
7+
*.py[cod]
8+
*$py.class
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
.Python
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
wheels/
27+
share/python-wheels/
28+
*.egg-info/
29+
.installed.cfg
30+
*.egg
31+
MANIFEST
32+
33+
# PyInstaller
34+
# Usually these files are written by a python script from a template
35+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
36+
*.manifest
37+
*.spec
38+
39+
# Installer logs
40+
pip-log.txt
41+
pip-delete-this-directory.txt
42+
43+
# Unit test / coverage reports
44+
htmlcov/
45+
.tox/
46+
.nox/
47+
.coverage
48+
.coverage.*
49+
.cache
50+
nosetests.xml
51+
coverage.xml
52+
*.cover
53+
*.py,cover
54+
.hypothesis/
55+
.pytest_cache/
56+
cover/
57+
58+
# Translations
59+
*.mo
60+
*.pot
61+
62+
# Django stuff:
63+
*.log
64+
local_settings.py
65+
db.sqlite3
66+
db.sqlite3-journal
67+
68+
# Flask stuff:
69+
instance/
70+
.webassets-cache
71+
72+
# Scrapy stuff:
73+
.scrapy
74+
75+
# Sphinx documentation
76+
docs/_build/
77+
78+
# PyBuilder
79+
.pybuilder/
80+
target/
81+
82+
# Jupyter Notebook
83+
.ipynb_checkpoints
84+
85+
# IPython
86+
profile_default/
87+
ipython_config.py
88+
89+
# pyenv
90+
# For a library or package, you might want to ignore these files since the code is
91+
# intended to run in multiple environments; otherwise, check them in:
92+
# .python-version
93+
94+
# pipenv
95+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
96+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
97+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
98+
# install all needed dependencies.
99+
#Pipfile.lock
100+
101+
# poetry
102+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
103+
# This is especially recommended for binary packages to ensure reproducibility, and is more
104+
# commonly ignored for libraries.
105+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
106+
#poetry.lock
107+
108+
# pdm
109+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
110+
#pdm.lock
111+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
112+
# in version control.
113+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
114+
.pdm.toml
115+
.pdm-python
116+
.pdm-build/
117+
118+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
119+
__pypackages__/
120+
121+
# Celery stuff
122+
celerybeat-schedule
123+
celerybeat.pid
124+
125+
# SageMath parsed files
126+
*.sage.py
127+
128+
# Environments
129+
.env
130+
.venv
131+
env/
132+
venv/
133+
ENV/
134+
env.bak/
135+
venv.bak/
136+
137+
# Spyder project settings
138+
.spyderproject
139+
.spyproject
140+
141+
# Rope project settings
142+
.ropeproject
143+
144+
# mkdocs documentation
145+
/site
146+
147+
# mypy
148+
.mypy_cache/
149+
.dmypy.json
150+
dmypy.json
151+
152+
# Pyre type checker
153+
.pyre/
154+
155+
# pytype static type analyzer
156+
.pytype/
157+
158+
# Cython debug symbols
159+
cython_debug/
160+
161+
# PyCharm
162+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
163+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
164+
# and can be added to the global gitignore or merged into this file. For a more nuclear
165+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
166+
#.idea/
167+
168+
*.pdf
169+
.vscode/
170+
.devcontainer/

build_my_notebook.py

Lines changed: 0 additions & 94 deletions
This file was deleted.

diary_generator/__main__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import sys
2+
3+
from diary_generator.example_diary import create_example_diary
4+
5+
6+
def main():
7+
create_example_diary(sys.argv[1])
8+
9+
10+
if __name__ == "__main__":
11+
main()

diary_generator/classes/doc.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from typing import Optional
2+
3+
import fitz
4+
5+
from diary_generator.classes.page import Page
6+
from diary_generator.classes.table_of_content_entry import TableOfContentEntry
7+
8+
9+
class Doc:
10+
# Represents the output document
11+
# This class exists primarily to collect the invidual pages, in the correct order.
12+
# As the intra pdf linking scheme relies on page number, all pages must be known
13+
# before links are created.
14+
#
15+
# Requires the path to a tempate pdf file when created:
16+
# the first page of this file will be used as the default
17+
# template for each page created, if the page itself does
18+
# not have a dedicated template.
19+
pages: list[Page]
20+
fitz_doc: fitz.Document
21+
toc: list[TableOfContentEntry]
22+
23+
def __init__(self, base_pdf_name: str):
24+
self.pages = []
25+
self.fitz_doc = fitz.open()
26+
self.base_pdf_name = base_pdf_name
27+
self.toc = []
28+
29+
def add_page(
30+
self,
31+
base_pdf_name: Optional[str] = None,
32+
title="",
33+
title_x=20,
34+
title_y=250,
35+
title_size=50,
36+
toc_level=0,
37+
):
38+
base_pdf = self._get_base_pdf_for_page(base_pdf_name)
39+
page = Page(
40+
base_pdf,
41+
title=title,
42+
title_x=title_x,
43+
title_y=title_y,
44+
title_size=title_size,
45+
toc_level=toc_level,
46+
)
47+
self.addPages(page)
48+
return page
49+
50+
def _get_base_pdf_for_page(self, base_pdf_name: Optional[str]) -> fitz.Document:
51+
if base_pdf_name is None:
52+
return fitz.open(self.base_pdf_name)
53+
else:
54+
return fitz.open(base_pdf_name)
55+
56+
# Add one or more pages into the document.
57+
# This method will create fitz pages for each doc, however rendering of content
58+
# is done as a separate pass
59+
def addPages(self, *pages: Page):
60+
for page in pages:
61+
self.pages.append(page)
62+
page.page_number = len(self.pages) - 1
63+
# copy tempate into new doc
64+
self.fitz_doc.insert_pdf(
65+
page.base_pdf,
66+
from_page=0,
67+
to_page=0,
68+
start_at=-1,
69+
rotate=-1,
70+
links=True,
71+
annots=True,
72+
show_progress=0,
73+
final=1,
74+
)
75+
if page.toc_level != 0:
76+
self.toc.append((page.toc_level, page.title, page.page_number))
77+
78+
# Ask each page to render their own conent- this is done once all pages are added
79+
# After rendering the document is saved.
80+
def render(self, output_file_name: str):
81+
for page in self.pages:
82+
page.render(self.fitz_doc)
83+
self.fitz_doc.set_toc(self.toc, collapse=1) # type: ignore
84+
self.fitz_doc.save(output_file_name)

0 commit comments

Comments
 (0)