create_docx.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import configure
  2. import os
  3. env_dict = os.environ
  4. henglish_conf = env_dict.get("HENGLISH_CONF")
  5. if henglish_conf is not None:
  6. configure.configure(henglish_conf, encoding="utf-8")
  7. try:
  8. from core.db import WordDatabase, word
  9. from docx import Document, shared, enum
  10. from docx.enum import section, text
  11. from docx.oxml.ns import qn
  12. except ImportError:
  13. print("Run `pip install python-docx` first.")
  14. else:
  15. def make_word(title: str, little_title: str, db: WordDatabase, font0=15, font1=8, font2=7, start=0, end=300,
  16. file_name="English"):
  17. doc = Document()
  18. sec = doc.sections[0] # sections对应文档中的“节”
  19. normal_style = doc.styles['Normal']
  20. normal_style.font.name = u'微软雅黑' # 必须先设置font.name
  21. normal_style.element.rPr.rFonts.set(qn('w:eastAsia'), u'微软雅黑')
  22. normal_style.font.color.rgb = shared.RGBColor(0, 0, 0)
  23. h1_style = doc.styles['Heading 1']
  24. h1_style.font.name = u'微软雅黑' # 必须先设置font.name
  25. h1_style.element.rPr.rFonts.set(qn('w:eastAsia'), u'微软雅黑')
  26. h1_style.font.size = shared.Pt(30)
  27. h1_style.font.color.rgb = shared.RGBColor(0, 0, 0)
  28. h2_style = doc.styles['Heading 2']
  29. h2_style.font.name = u'微软雅黑' # 必须先设置font.name
  30. h2_style.element.rPr.rFonts.set(qn('w:eastAsia'), u'微软雅黑')
  31. h2_style.font.size = shared.Pt(font0)
  32. h2_style.font.color.rgb = shared.RGBColor(0, 0, 0)
  33. sec.left_margin = shared.Cm(3.18) # 以下依次设置左、右、上、下页面边距
  34. sec.right_margin = shared.Cm(3.18)
  35. sec.top_margin = shared.Cm(1.27)
  36. sec.bottom_margin = shared.Cm(1.27)
  37. sec.orientation = section.WD_ORIENTATION.LANDSCAPE # 设置纵向
  38. sec.page_width = shared.Cm(21) # 设置页面宽度
  39. sec.page_height = shared.Cm(29.7) # 设置页面高度
  40. sec.different_first_page_header_footer = True
  41. title_par = doc.add_heading(level=1)
  42. title_par.paragraph_format.alignment = text.WD_PARAGRAPH_ALIGNMENT.CENTER
  43. title_run = title_par.add_run(title)
  44. title_run.add_break(text.WD_BREAK.PAGE)
  45. header = sec.header.paragraphs[0] # 获取第一个节的页眉
  46. header.paragraph_format.alignment = text.WD_PARAGRAPH_ALIGNMENT.CENTER
  47. l_title = header.add_run(little_title) # 添加页面内容
  48. l_title.font.size = shared.Pt(6)
  49. footer = sec.footer.paragraphs[0] # 获取第一个节的页眉
  50. footer.paragraph_format.alignment = text.WD_PARAGRAPH_ALIGNMENT.CENTER
  51. power_bt = footer.add_run(f"Power HEnglish! https://github.com/SuperH-0630/HEnglish") # 添加页面内容
  52. power_bt.font.size = shared.Pt(6)
  53. def add_word(w: word.Word):
  54. doc.add_heading(w.name, level=2) # 添加标题
  55. for i in w.comment:
  56. c: word.Word.Comment = w.comment[i]
  57. paragraph = doc.add_paragraph()
  58. part = paragraph.add_run(c.part + " ")
  59. part.italic = True
  60. part.bold = True
  61. part.font.size = shared.Pt(font1)
  62. english = paragraph.add_run(c.english)
  63. english.bold = True
  64. english.font.size = shared.Pt(font1)
  65. chinese = paragraph.add_run(c.chinese)
  66. chinese.bold = True
  67. chinese.font.size = shared.Pt(font1)
  68. for eg_index, e in enumerate(c.eg):
  69. e = e.split("##")
  70. english_sentence = paragraph.add_run(f"Eg {eg_index + 1}: " + e[0])
  71. english_sentence.font.size = shared.Pt(font2)
  72. chinese_sentence = paragraph.add_run(" " + e[1] + "")
  73. chinese_sentence.font.size = shared.Pt(font2)
  74. ph_format = paragraph.paragraph_format
  75. ph_format.line_spacing_rule = text.WD_LINE_SPACING.SINGLE # 设置行间距
  76. last_ten = -1
  77. index = start
  78. while True:
  79. w = db.find_word_by_index(index)
  80. if w is None:
  81. break
  82. add_word(w)
  83. index += 1
  84. if index // 10 > last_ten:
  85. last_ten = index // 10
  86. print(index)
  87. if index == end:
  88. break
  89. doc.save(f'{file_name}.docx')
  90. return index
  91. word_db = WordDatabase("high_school", configure.conf["DB_TEMPLATE"])
  92. page = 0
  93. step = 500
  94. book = 1
  95. while True:
  96. print(f"Book: {book}")
  97. ret = make_word(f"\n\n\nHigh School English\nWord - {book}",
  98. f"High School English Word - {book}",
  99. word_db,
  100. start=page,
  101. end=page + step,
  102. file_name=f"English{book}")
  103. if ret != page + step:
  104. break
  105. page += step
  106. book += 1