-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptpython2pdf_extract.py
113 lines (104 loc) · 3.63 KB
/
scriptpython2pdf_extract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=line-too-long
# end of Python header, start of markdown py2pdf
# ~~ ---
# ~~ title : Automatic pdf report from a python script with pandoc
# ~~ author : ChristianV
# ~~ date : 14 Oct 2022
# ~~ abstract : Export a pdf report (code, outputs) commented with the Markdown embedded in the python script, a bash preprocessing and pandoc. Everything is launched whithin the python script.
# ~~ lang: en-US
# ~~ geometry:
# ~~ - margin = 2cm
# ~~ - a4paper
# ~~ toc: true
# ~~ toc_depth: 2
# ~~ numbersections: true
# ~~ links-as-notes: false
# ~~ linkcolor: blue
# ~~ ---
# ~~
# ~~ # Which problem does this solve?
# ~~
# ~~ To have the possibility to make a report
# ~~..../
# Some more lines here
# ~~./...
# ~~ Some code is also needed to store the desired matplotlib graphs.
# ~~
# ~~ The command lines are embedded in a bash file called at the end of the script. In this example the bash file is in the same directory than the python script. For a more productive configuration, the bash file might be located in one directory, an alias added in ".bashrc". The call to script is then :
# ~~ ```
# ~~ py2pdf scriptname
# ~~ ```
# ~~ "py2pdf" being the alias.
# ~~
# ~~ Then in the ".bashrc" file :
# ~~ ```bash
# ~~ # my scripts
# ~~ p="$HOME/0_myscripts/" # or any other path to your scripts
# ~~ alias py2pdf="$p/py2pdf"
# ~~ ```
# ~~
# ~~ ### Resources
# ~~ * pandoc : [pandoc.org](https://pandoc.org/)
# ~~ * python, bash, perl : standard of Linux distribution?
# ~~
# ~~ # python code
# ~~
# ~~ ## Import modules
# ~~ ```python
# import
import os # for py2pdf
import subprocess # for py2pdf with bash with alias
import matplotlib.pyplot as plt # for the demo
import numpy as np # for the demo
# ~~ ```
# ~~
# ~~ ## Functions
# ~~
# ~~ This is a demo file. In other applications, for a better readability, those specific functions might be included in a library to import in the above section.
# ~~ ```python
def print_twice(log, *args, **kwargs):
# the double print function (to console and to log)
print(*args, **kwargs)
with open(log, "a") as f: # appends to file and closes it when finished
print(file=f, *args, **kwargs)
# ~~..../
# Some more lines here
# ~~./...
# ~~ ```
# ~~ ## Main code
# ~~
# ~~ ### Code section 1
# ~~ ```python
# lines to be included for py2pdf export
# get this script file name without extension
scriptname = os.path.basename(__file__).split(".")[0]
logfile = "./py2pdf_files/log1.txt" # define the logfile
outputdir() # create the directory to store the output
clearlog(logfile) # clear the logfile (in case script is ran several times)
# code for console output demo
path = os.getcwd() # get the path of the current directory
print_twice(logfile, "Path of the current directory : " + path)
print_twice(logfile, "python script name : ", scriptname)
print_twice(logfile, "Directory content :")
print_twice(logfile, "\n".join(os.listdir(path)))
print_twice(logfile, "Logfile : ", logfile)
# ~~ ```
# ~~ ### Code Output (section 1)
# ~~ ```
# ~~ ![[./py2pdf_files/log1.txt]]
# ~~ ```
# ~~..../
# Some more lines here
# ~~./...
# prepare the command to launch the report creation
# cmd = "./py2pdf " + scriptname # for a local bash
# cmd = "~/0_scripts/py2pdf " + scriptname # for a bash in directory in home
cmd = "py2pdf " + scriptname # alias
# code for console output demo
print_twice(logfile, "launch command : ", cmd) # for demo
print_twice(logfile, "pdf should be available soon") # for demo
# launch the report creation
# os.system(cmd) # to launch the bash file (local or directory in home)
subprocess.call(["/bin/bash", "-i", "-c", cmd]) # to launch the bash file (alias)