Spaces:
Runtime error
Runtime error
File size: 6,172 Bytes
3521eb6 | 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | import json
import os
import argparse
from md2pdf.core import md2pdf
def json_to_markdown(json_data, filename):
"""
Convert JSON data into a Markdown string.
:param json_data: List of dictionaries each containing
"start", "end", "text", "speaker", "language"
:param filename: The name of the JSON file (used as the main title)
:return: A string of valid Markdown
"""
# Title
title = filename.replace("-paragraphs.json", "")
md_output = f"# {title}\n\n"
# Transcription Header
md_output += "## Transcription\n\n"
# Build the content for each contribution
for entry in json_data:
speaker = entry.get("speaker", "Unknown Speaker")
language = entry.get("language", "Unknown Language")
start = entry.get("start", "")
end = entry.get("end", "")
text = entry.get("text", "")
# Speaker-language heading
md_output += f"### {speaker} - {language}\n\n"
# Table for start/end times
md_output += "| start | end |\n"
md_output += "|-------------|-------------|\n"
md_output += f"| {start} | {end} |\n\n"
# Add the text below the table
md_output += f"{text}\n\n"
return md_output
def save_markdown_to_file(markdown_text, output_md_path):
"""
Save the Markdown text to a file.
:param markdown_text: The Markdown content as a string.
:param output_md_path: The path (including filename) where the .md should be saved.
"""
with open(output_md_path, 'w', encoding='utf-8') as md_file:
md_file.write(markdown_text)
def markdown_to_pdf(markdown_text, output_pdf_path):
"""
Convert a Markdown string to a PDF using md2pdf.
:param markdown_text: The Markdown text to convert.
:param output_pdf_path: The path (including filename) where the PDF should be saved.
"""
# Custom CSS (adapt as you like)
custom_css = r"""
@font-face {
font-family: "Bitstream Vera Serif Bold";
src: url("https://mdn.github.io/css-examples/web-fonts/VeraSeBd.ttf");
}
body {
margin: 0 auto;
background-color: white;
font-family: "Bitstream Vera Serif Bold";
color: #333333;
line-height: 1;
max-width: 800px;
padding: 30px;
font-size: 12px;
}
p {
line-height: 150%;
max-width: 960px;
font-weight: 400;
color: #333333;
}
h1,
h2,
h3,
h4 {
font-weight: 400;
}
h2,
h3,
h4,
h5,
p {
margin-bottom: 25px;
padding: 0;
}
h1 {
margin-bottom: 10px;
font-size: 300%;
padding: 0px;
}
h2 {
font-size: 150%;
}
h3 {
font-size: 120%;
}
h4 {
font-size: 100%;
}
h5 {
font-size: 80%;
font-weight: 100;
}
h6 {
font-size: 80%;
font-weight: 100;
color: red;
}
a {
color: grey;
margin: 0;
padding: 0;
vertical-align: baseline;
}
a:hover {
text-decoration: blink;
color: green;
}
a:visited {
color: black;
}
ul,
ol {
padding: 0;
margin: 0px 0px 0px 50px;
}
ul {
list-style-type: square;
list-style-position: inside;
}
li {
line-height: 150%;
}
li ul,
li ul {
margin-left: 24px;
}
pre {
padding: 0px 24px;
max-width: 800px;
white-space: pre-wrap;
}
code {
font-family: Consolas, Monaco, Andale Mono, monospace;
line-height: 1.5;
font-size: 13px;
}
aside {
display: block;
float: right;
width: 390px;
}
blockquote {
border-left: 0.5em solid #eee;
padding: 0 1em;
margin-left: 0;
max-width: 476px;
}
blockquote cite {
line-height: 20px;
color: #bfbfbf;
}
blockquote cite:before {
content: "\2014 \00A0";
}
blockquote p {
color: #666;
max-width: 460px;
}
hr {
text-align: left;
margin: 0 auto 0 0;
color: #999;
}
/* Table styling for the start/end times */
table {
border-collapse: collapse;
width: 100%;
margin-bottom: 15px;
}
table, th, td {
border: 1px solid #333;
padding: 6px;
}
th {
background-color: #eee;
text-align: left;
}
"""
# Convert Markdown to PDF directly from a string
md2pdf(output_pdf_path,
md_content=markdown_text, # We pass the markdown text as "raw"
css_file_path=None, # If you have an external .css file, you can pass its path here
base_url=None # If you have images or relative links
)
# If we want the custom CSS from a file, we can write it to a temporary file:
# Optionally, you could do something like this:
#
# with open("custom_style.css", "w", encoding="utf-8") as css_file:
# css_file.write(custom_css)
#
# md2pdf(output_pdf_path,
# raw=markdown_text,
# css="custom_style.css", # pass the CSS file path
# extras=[],
# base_url=None
# )
def parse_args():
"""
Parse command-line arguments using argparse.
"""
parser = argparse.ArgumentParser(
description="Convert a JSON-based transcription to Markdown and then to PDF using md2pdf."
)
parser.add_argument("input_filename",
help="Path to the JSON file containing transcription data.")
parser.add_argument("--output_md",
default="transcription.md",
help="Output Markdown file name (default: transcription.md)")
parser.add_argument("--output_pdf",
default="transcription.pdf",
help="Output PDF file name (default: transcription.pdf)")
return parser.parse_args()
def convert_json_to_pdf(input_filename, output_md="transcription.md", output_pdf="transcription.pdf"):
# 1. Read JSON data
with open(input_filename, 'r', encoding='utf-8') as f:
data = json.load(f)
# 2. Convert JSON to Markdown
markdown_output = json_to_markdown(data, os.path.basename(input_filename))
# 3. Save Markdown to a .md file (optional)
save_markdown_to_file(markdown_output, output_md)
print(f"Markdown saved to: {output_md}")
# 4. Convert Markdown to PDF using md2pdf
markdown_to_pdf(markdown_output, output_pdf)
print(f"PDF generated and saved to: {output_pdf}")
def main():
args = parse_args()
convert_json_to_pdf(args.input_filename, args.output_md, args.output_pdf)
if __name__ == '__main__':
main() |