pydocmaker 2.3.2__tar.gz → 2.3.3__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.
- {pydocmaker-2.3.2/src/pydocmaker.egg-info → pydocmaker-2.3.3}/PKG-INFO +1 -1
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/__init__.py +2 -2
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/backend/ex_docx.py +58 -3
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/backend/pandoc_api.py +21 -1
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/core.py +5 -2
- {pydocmaker-2.3.2 → pydocmaker-2.3.3/src/pydocmaker.egg-info}/PKG-INFO +1 -1
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/LICENSE +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/README.md +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/setup.cfg +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/setup.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/backend/__init__.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/backend/baseformatter.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/backend/ex_html.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/backend/ex_ipynb.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/backend/ex_markdown.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/backend/ex_redmine.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/backend/ex_tex.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/backend/mdx_latex.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/backend/pdf_maker.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/templating.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker/util.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker.egg-info/SOURCES.txt +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker.egg-info/dependency_links.txt +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker.egg-info/requires.txt +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/src/pydocmaker.egg-info/top_level.txt +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/tests/test_backend_pandoc.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/tests/test_convert_all.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/tests/test_core.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/tests/test_ex_html.py +0 -0
- {pydocmaker-2.3.2 → pydocmaker-2.3.3}/tests/test_util.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pydocmaker
|
|
3
|
-
Version: 2.3.
|
|
3
|
+
Version: 2.3.3
|
|
4
4
|
Summary: a minimal document maker to make docx, markdown, html, textile, redmine, and tex documents from python. Written in pure python.
|
|
5
5
|
Home-page: https://github.com/TobiasGlaubach/pydocmaker
|
|
6
6
|
Author: Tobias Glaubach
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
__version__ = '2.3.
|
|
1
|
+
__version__ = '2.3.3'
|
|
2
2
|
|
|
3
3
|
from pydocmaker.core import DocBuilder, construct, constr, buildingblocks, print_to_pdf, get_latex_compiler, set_latex_compiler, make_pdf_from_tex, show_pdf
|
|
4
4
|
from pydocmaker.util import upload_report_to_redmine, bcolors, txtcolor, colors_dc
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
from pydocmaker.backend.ex_docx import edit_docx_xml
|
|
7
7
|
from pydocmaker.backend.ex_tex import can_run_pandoc
|
|
8
8
|
from pydocmaker.backend.pdf_maker import get_all_installed_latex_compilers, get_latex_compiler
|
|
9
9
|
from pydocmaker.backend.pandoc_api import pandoc_convert_file, pandoc_set_allowed
|
|
@@ -10,6 +10,10 @@ from docx.shared import Inches, Pt
|
|
|
10
10
|
import tempfile
|
|
11
11
|
import os
|
|
12
12
|
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
import zipfile, os, sys
|
|
15
|
+
from io import BytesIO
|
|
16
|
+
|
|
13
17
|
import markdown
|
|
14
18
|
|
|
15
19
|
try:
|
|
@@ -32,6 +36,57 @@ except Exception as err:
|
|
|
32
36
|
|
|
33
37
|
|
|
34
38
|
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def edit_docx_xml(file_path, replace_dict, output_file_or_buffer=None):
|
|
42
|
+
"""
|
|
43
|
+
Edit raw XML content of a DOCX file by replacing specified strings in all XML files within the document.
|
|
44
|
+
|
|
45
|
+
This function reads a DOCX file, extracts its contents (which are stored as a ZIP archive),
|
|
46
|
+
searches for specific strings in all XML files inside the archive, replaces them with new values,
|
|
47
|
+
and writes the modified content back into a new DOCX file or returns it as bytes.
|
|
48
|
+
Args:
|
|
49
|
+
file_path (str): Path to the input DOCX file to be modified
|
|
50
|
+
replace_dict (dict): Dictionary mapping strings to be replaced (keys) to their replacement values (values)
|
|
51
|
+
output_file_or_buffer (str, Path, or buffer object, optional): Path to save the modified DOCX file, or a buffer object to write to. If None, returns the modified DOCX content as bytes.
|
|
52
|
+
Returns:
|
|
53
|
+
bool or bytes: If output_file_or_buffer is a path, returns True if successful. If output_file_or_buffer is a buffer or None, returns the modified DOCX content as bytes.
|
|
54
|
+
"""
|
|
55
|
+
# Read the original DOCX file
|
|
56
|
+
with open(file_path, 'rb') as f:
|
|
57
|
+
docx_data = f.read()
|
|
58
|
+
|
|
59
|
+
# Create a temporary zip file from the DOCX data
|
|
60
|
+
zip_buffer = BytesIO(docx_data)
|
|
61
|
+
with zipfile.ZipFile(zip_buffer, 'r') as zip_file:
|
|
62
|
+
# Get all file names in the archive
|
|
63
|
+
file_list = zip_file.namelist()
|
|
64
|
+
|
|
65
|
+
output_buffer = BytesIO()
|
|
66
|
+
with zipfile.ZipFile(output_buffer, 'w', zipfile.ZIP_DEFLATED) as new_zip:
|
|
67
|
+
# Process each file in the original zip
|
|
68
|
+
for filename in file_list:
|
|
69
|
+
# Read the file content
|
|
70
|
+
content = zip_file.read(filename)
|
|
71
|
+
|
|
72
|
+
for key_to_replace, new_value in replace_dict.items():
|
|
73
|
+
content = content.replace(key_to_replace.encode('utf-8'), new_value.encode('utf-8'))
|
|
74
|
+
|
|
75
|
+
# Add file to new zip
|
|
76
|
+
new_zip.writestr(filename, content)
|
|
77
|
+
# Create a new zip file in memory
|
|
78
|
+
|
|
79
|
+
if isinstance(output_file_or_buffer, (str, Path)):
|
|
80
|
+
os.makedirs(os.path.dirname(output_file_or_buffer), exist_ok=True)
|
|
81
|
+
with open(output_file_or_buffer, 'wb') as output_buffer:
|
|
82
|
+
output_buffer.write(output_buffer.getvalue())
|
|
83
|
+
return os.path.exists(output_file_or_buffer)
|
|
84
|
+
elif hasattr(output_file_or_buffer, 'write'):
|
|
85
|
+
return output_buffer.write(output_buffer.getvalue())
|
|
86
|
+
else:
|
|
87
|
+
return output_buffer.getvalue()
|
|
88
|
+
|
|
89
|
+
|
|
35
90
|
def blue(run):
|
|
36
91
|
run.font.color.rgb = docx.shared.RGBColor(0, 0, 255)
|
|
37
92
|
|
|
@@ -51,12 +106,12 @@ def convert_pandoc(doc:List[dict]) -> bytes:
|
|
|
51
106
|
with open(docx_file_path, 'rb') as fp:
|
|
52
107
|
return fp.read()
|
|
53
108
|
|
|
54
|
-
def convert(doc:List[dict]) -> bytes:
|
|
109
|
+
def convert(doc:List[dict], template_path:str=None, make_blue=False) -> bytes:
|
|
55
110
|
|
|
56
|
-
if can_run_pandoc():
|
|
111
|
+
if can_run_pandoc() and not template_path:
|
|
57
112
|
return convert_pandoc(doc)
|
|
58
113
|
else:
|
|
59
|
-
renderer = docx_renderer()
|
|
114
|
+
renderer = docx_renderer(template_path, make_blue=make_blue)
|
|
60
115
|
renderer.digest(doc)
|
|
61
116
|
return renderer.doc_to_bytes()
|
|
62
117
|
|
|
@@ -71,6 +71,26 @@ def pandoc_set_allowed(is_allowed):
|
|
|
71
71
|
allow_pandoc = True if is_allowed else False
|
|
72
72
|
return allow_pandoc
|
|
73
73
|
|
|
74
|
+
def pandoc_merge_files(inp_files, out_file):
|
|
75
|
+
"""
|
|
76
|
+
Convert a file using pandoc.
|
|
77
|
+
|
|
78
|
+
Parameters:
|
|
79
|
+
inp_files (List[str]): The path to the input file.
|
|
80
|
+
out_file (str or Path): The path to the output file or the desired output format.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
subprocess.CompletedProcess: The result of the pandoc conversion command.
|
|
84
|
+
|
|
85
|
+
Raises:
|
|
86
|
+
AssertionError: If the input file does not exist or if no output file or format is provided.
|
|
87
|
+
"""
|
|
88
|
+
for inp_file in inp_files:
|
|
89
|
+
assert inp_file, "Need to give an input file name!"
|
|
90
|
+
assert os.path.exists(inp_file), f"input file {inp_file=} does not exist!"
|
|
91
|
+
|
|
92
|
+
assert out_file, "Need to give an output file name!"
|
|
93
|
+
return subprocess.run(['pandoc', *inp_files, '-o', out_file])
|
|
74
94
|
|
|
75
95
|
def pandoc_convert_file(inp_file, out_file_or_format):
|
|
76
96
|
"""
|
|
@@ -89,7 +109,7 @@ def pandoc_convert_file(inp_file, out_file_or_format):
|
|
|
89
109
|
AssertionError: If the input file does not exist or if no output file or format is provided.
|
|
90
110
|
"""
|
|
91
111
|
|
|
92
|
-
assert inp_file, "Need to give an
|
|
112
|
+
assert inp_file, "Need to give an input file name!"
|
|
93
113
|
assert os.path.exists(inp_file), f"input file {inp_file=} does not exist!"
|
|
94
114
|
|
|
95
115
|
out_file = out_file_or_format
|
|
@@ -986,17 +986,20 @@ class DocBuilder(UserList):
|
|
|
986
986
|
"""
|
|
987
987
|
return self._ret(to_markdown(self.dump(), embed_images=embed_images), path_or_stream)
|
|
988
988
|
|
|
989
|
-
def to_docx(self, path_or_stream=None) -> bytes:
|
|
989
|
+
def to_docx(self, path_or_stream=None, template_path:str=None, make_blue=False) -> bytes:
|
|
990
990
|
"""
|
|
991
991
|
Converts the current object to a DOCX file.
|
|
992
992
|
|
|
993
993
|
Args:
|
|
994
994
|
path_or_stream (str or io.IOBase, optional): The path to save the file to, or a file-like object to write the data to. If not provided, the data will be returned as string.
|
|
995
|
+
template_path (str, optional): Path to a template file to use for formatting the DOCX output (will append to the end of the document).
|
|
996
|
+
make_blue (bool): If True, applies blue color styling to the generated document.
|
|
997
|
+
|
|
995
998
|
|
|
996
999
|
Returns:
|
|
997
1000
|
bytes: The data as bytes, or True if the data was saved successfully to a file or stream.
|
|
998
1001
|
"""
|
|
999
|
-
return self._ret(to_docx(self.dump()), path_or_stream)
|
|
1002
|
+
return self._ret(to_docx(self.dump(), template_path=template_path, make_blue=make_blue), path_or_stream)
|
|
1000
1003
|
|
|
1001
1004
|
def to_ipynb(self, path_or_stream=None) -> str:
|
|
1002
1005
|
"""
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pydocmaker
|
|
3
|
-
Version: 2.3.
|
|
3
|
+
Version: 2.3.3
|
|
4
4
|
Summary: a minimal document maker to make docx, markdown, html, textile, redmine, and tex documents from python. Written in pure python.
|
|
5
5
|
Home-page: https://github.com/TobiasGlaubach/pydocmaker
|
|
6
6
|
Author: Tobias Glaubach
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|