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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
import docx from docx import Document from docx import opc from docx.oxml.ns import qn from docx.oxml import OxmlElement from datetime import datetime from docx.shared import Inches, Pt, RGBColor from docx.enum.text import WD_ALIGN_PARAGRAPH import sys, os
font_header_name = "Calibri" font_header_size = Pt(14) font_header_color = RGBColor(64, 64, 64)
font_chapter_name = "Calibri" font_chapter_size = Pt(12) font_chapter_color = RGBColor(128, 128, 128)
font_body_name = "Calibri" font_body_size = Pt(10) font_body_color = RGBColor(0, 0, 0)
def insertHR(paragraph, color): p = paragraph._p pPr = p.get_or_add_pPr() pBdr = OxmlElement('w:pBdr') pPr.insert_element_before(pBdr, 'w:shd', 'w:tabs', 'w:suppressAutoHyphens', 'w:kinsoku', 'w:wordWrap', 'w:overflowPunct', 'w:topLinePunct', 'w:autoSpaceDE', 'w:autoSpaceDN', 'w:bidi', 'w:adjustRightInd', 'w:snapToGrid', 'w:spacing', 'w:ind', 'w:contextualSpacing', 'w:mirrorIndents', 'w:suppressOverlap', 'w:jc', 'w:textDirection', 'w:textAlignment', 'w:textboxTightWrap', 'w:outlineLvl', 'w:divId', 'w:cnfStyle', 'w:rPr', 'w:sectPr', 'w:pPrChange' ) bottom = OxmlElement('w:bottom') bottom.set(qn('w:val'), 'single') bottom.set(qn('w:sz'), '12') bottom.set(qn('w:space'), '1') bottom.set(qn('w:color'), color) pBdr.append(bottom)
def add_hyperlink(paragraph, url, text, color="#0000ff", underline=True): """ A function that places a hyperlink within a paragraph object.
:param paragraph: The paragraph we are adding the hyperlink to. :param url: A string containing the required url :param text: The text displayed for the url :return: The hyperlink object """
part = paragraph.part r_id = part.relate_to(url, docx.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_external=True)
hyperlink = docx.oxml.shared.OxmlElement('w:hyperlink') hyperlink.set(docx.oxml.shared.qn('r:id'), r_id, )
new_run = docx.oxml.shared.OxmlElement('w:r')
rPr = docx.oxml.shared.OxmlElement('w:rPr')
if not color is None: c = docx.oxml.shared.OxmlElement('w:color') c.set(docx.oxml.shared.qn('w:val'), color) rPr.append(c)
if not underline: u = docx.oxml.shared.OxmlElement('w:u') u.set(docx.oxml.shared.qn('w:val'), 'none') rPr.append(u)
new_run.append(rPr) new_run.text = text hyperlink.append(new_run)
paragraph._p.append(hyperlink)
return hyperlink
def apply_style(paragraph, style, font_name=None, font_size=None, font_color=None): if style == "header": paragraph.style.font.name = font_header_name paragraph.style.font.size = font_header_size paragraph.style.font.color.rgb = font_header_color elif style == "chapter": paragraph.style.font.name = font_chapter_name paragraph.style.font.size = font_chapter_size paragraph.style.font.color.rgb = font_chapter_color elif style == "body": paragraph.style.font.name = font_body_name paragraph.style.font.size = font_body_size paragraph.style.font.color.rgb = font_body_color
if font_name: paragraph.style.font.name = font_name if font_size: paragraph.style.font.size = font_size if font_color: paragraph.style.font.color.rgb = font_color
def init_doc(title, year, authors, journal="", volume="", issue="", pageRange="", articleNo="", doi=""): doc = Document()
para_title = doc.add_heading(title, 1) para_title.style.font.name = font_header_name para_title.style.font.size = font_header_size para_title.style.font.color.rgb = font_header_color insertHR(para_title, "#808096")
para_authors = doc.add_paragraph(style='List Bullet') para_authors.paragraph_format.left_indent = Inches(0.5) para_authors.style.font.name = font_body_name para_authors.style.font.size = font_body_size para_authors.style.font.color.rgb = font_body_color run = para_authors.add_run(authors) run.italic=True journal_info = f"{journal} {volume}" if issue and len(issue) > 1: journal_info += f", ({issue})" if pageRange and len(pageRange) > 1: journal_info += f", {pageRange}" if articleNo and len(articleNo) > 1: journal_info += f", {articleNo}" journal_info += f" ({year})" para_journal_info = doc.add_paragraph(journal_info, style="List Bullet") para_journal_info.paragraph_format.left_indent = Inches(0.5) para_journal_info.style.font.name = font_body_name para_journal_info.style.font.size = font_body_size para_journal_info.style.font.color.rgb = font_body_color
if len(doi) > 1 and not doi.startswith("https://doi.org/"): doi = "https://doi.org/" + doi elif len(doi) > 1 and doi.startswith("https://doi.org/"): doi = doi if len(doi) > 1: para_doi = doc.add_paragraph("DOI: ", style="List Bullet") para_doi.paragraph_format.left_indent = Inches(0.5) para_doi.style.font.name = font_body_name para_doi.style.font.size = font_body_size para_doi.style.font.color.rgb = font_body_color add_hyperlink(para_doi, doi, doi) else: pass
run = para_doi.add_run() run.add_break()
return doc
def set_cell_border(cell, **kwargs): """ Set cell border """ tc = cell._tc tcPr = tc.get_or_add_tcPr()
for edge in ['top', 'left', 'bottom', 'right']: edge_data = kwargs.get(edge) if edge_data: tag = 'w:{}'.format(edge) element = OxmlElement(tag) element.set(qn('w:val'), edge_data.get('val', 'single')) element.set(qn('w:sz'), str(edge_data.get('sz', 4))) element.set(qn('w:space'), str(edge_data.get('space', 0))) color = edge_data.get('color', 'auto') if isinstance(color, tuple): color = '%02x%02x%02x' % color element.set(qn('w:color'), color) tcPr.append(element)
def add_reference_table(doc, references): for ref in references: table = doc.add_table(rows=1, cols=1) table.style = 'Table Grid' cell = table.cell(0, 0)
gray_color = (128, 128, 128) set_cell_border( cell, top={"sz": 4, "val": "single", "color": gray_color}, bottom={"sz": 4, "val": "single", "color": gray_color}, start={"sz": 4, "val": "single", "color": gray_color}, end={"sz": 4, "val": "single", "color": gray_color} ) p_point = cell.paragraphs[0] run = p_point.add_run(f" ※ {ref['citation point']}") p_point.style.font.name = font_body_name p_point.style.font.size = font_body_size run.bold = True
p = cell.add_paragraph(style="List Bullet") p.paragraph_format.left_indent = Inches(0.5) if isinstance(ref['authors'], list): ref['authors'] = ", ".join(ref['authors']) else: pass p.add_run(f"{ref['authors']}") p.style.font.name = font_body_name p.style.font.size = font_body_size p.style.font.color.rgb = font_body_color
p = cell.add_paragraph(style="List Bullet") p.add_run(f"\"{ref['title']}\"") p.paragraph_format.left_indent = Inches(0.5) p.style.font.name = font_body_name p.style.font.size = font_body_size p.style.font.color.rgb = font_body_color
p = cell.add_paragraph(style="List Bullet") run = p.add_run(f"{ref['journal']} ") run.italic = True p.add_run(f"({ref['year']}). ") p.paragraph_format.left_indent = Inches(0.5) p.style.font.name = font_body_name p.style.font.size = font_body_size p.style.font.color.rgb = font_body_color
add_hyperlink(p, f"https://doi.org/{ref['doi']}", ref['doi'])
cell.add_paragraph()
return doc
def add_content(doc, purpose, contribution_academic, contribution_industrial, method_names, method_explanations, originality_names, originality_explanations, limitation_names, limitation_explanations, references): para = doc.add_paragraph() run = para.add_run("1. 연구 목적") run.bold = True para.style.font.name = font_chapter_name para.style.font.size = font_chapter_size para.style.font.color.rgb = font_chapter_color
para_purpose = doc.add_paragraph(style="List Bullet") para_purpose.paragraph_format.left_indent = Inches(0.5) para_purpose.add_run(purpose) apply_style(para_purpose, "body")
para = doc.add_paragraph() run = para.add_run("2. 학문적 및 산업적 기여") run.bold = True para.style.font.name = font_chapter_name para.style.font.size = font_chapter_size para.style.font.color.rgb = font_chapter_color
para_contribution_academic = doc.add_paragraph(style="List Bullet") para_contribution_academic.paragraph_format.left_indent = Inches(0.5) para_contribution_academic.style.font.name = font_body_name para_contribution_academic.style.font.size = font_body_size para_contribution_academic.style.font.color.rgb = font_body_color run = para_contribution_academic.add_run("학문적 기여: ") run.bold = True if isinstance(contribution_academic, list): para_contribution_academic.add_run( *contribution_academic ) else: para_contribution_academic.add_run( contribution_academic )
para_contribution_industrial = doc.add_paragraph(style="List Bullet") para_contribution_industrial.paragraph_format.left_indent = Inches(0.5) para_contribution_industrial.style.font.name = font_body_name para_contribution_industrial.style.font.size = font_body_size para_contribution_industrial.style.font.color.rgb = font_body_color run = para_contribution_industrial.add_run("산업적 기여: ") run.bold = True if isinstance(contribution_industrial, list): para_contribution_industrial.add_run( *contribution_industrial, ) else: para_contribution_industrial.add_run( contribution_industrial, )
para = doc.add_paragraph() run = para.add_run("3. 방법론") run.bold = True para.style.font.name = font_chapter_name para.style.font.size = font_chapter_size para.style.font.color.rgb = font_chapter_color for name, expl in zip(method_names, method_explanations): para_method = doc.add_paragraph(style="List Bullet") para_method.paragraph_format.left_indent = Inches(0.5) para_method.style.font.name = font_body_name para_method.style.font.size = font_body_size para_method.style.font.color.rgb = font_body_color run = para_method.add_run(f"{name}: ") run.bold = True para_method.add_run( expl )
para = doc.add_paragraph() run = para.add_run("4. 독창성") run.bold = True para.style.font.name = font_chapter_name para.style.font.size = font_chapter_size para.style.font.color.rgb = font_chapter_color for name, expl in zip(originality_names, originality_explanations): para_originality = doc.add_paragraph(style="List Bullet") para_originality.paragraph_format.left_indent = Inches(0.5) para_originality.style.font.name = font_body_name para_originality.style.font.size = font_body_size para_originality.style.font.color.rgb = font_body_color run = para_originality.add_run(f"{name}: ") run.bold = True para_originality.add_run( expl )
para = doc.add_paragraph() run = para.add_run("5. 한계점") run.bold = True para.style.font.name = font_chapter_name para.style.font.size = font_chapter_size para.style.font.color.rgb = font_chapter_color for name, expl in zip(limitation_names, limitation_explanations): para_limitation = doc.add_paragraph(style="List Bullet") para_limitation.paragraph_format.left_indent = Inches(0.5) para_limitation.style.font.name = font_body_name para_limitation.style.font.size = font_body_size para_limitation.style.font.color.rgb = font_body_color run = para_limitation.add_run(f"{name}: ") run.bold = True para_limitation.add_run( expl )
para = doc.add_paragraph() run = para.add_run("6. 주요 레퍼런스") run.bold = True para.style.font.name = font_chapter_name para.style.font.size = font_chapter_size para.style.font.color.rgb = font_chapter_color doc = add_reference_table(doc, references)
return doc
def save_doc(doc, authors, journal, year):
today_date = datetime.today().strftime('%Y%m%d') if isinstance(authors, str): authors = authors.split(",") filename = f"{today_date}_{authors[0].replace(' ', '')}_{journal.replace(' ', '')}_{year}.docx"
working_path = os.getcwd() filename = os.path.join(working_path, filename) doc.save(filename)
return filename
def gen_doc(title, year, authors, journal="", volume="", issue="", pageRange="", articleNo="", doi="", purpose="", contribution_academic="", contribution_industrial="", method_names=[], method_explanations=[], originality_names=[], originality_explanations=[], limitation_names=[], limitation_explanations=[], references=[]): doc = init_doc(title, year, authors, journal, volume, issue, pageRange, articleNo, doi) doc = add_content(doc, purpose, contribution_academic, contribution_industrial, method_names, method_explanations, originality_names, originality_explanations, limitation_names, limitation_explanations, references) filename = save_doc(doc, authors, journal, year) print(f"Document created: {filename}")
return filename
|