generate.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """Regenerate the sample documents used by tests.
  2. Run from this directory:
  3. uv run --with python-docx==1.1.2 --with openpyxl==3.1.5 --with python-pptx==1.0.2 --with reportlab==4.2.5 generate.py
  4. """
  5. from docx import Document
  6. from docx.shared import Pt
  7. from openpyxl import Workbook
  8. from openpyxl.styles import Font, PatternFill
  9. from pptx import Presentation
  10. from pptx.util import Inches, Pt as PptPt
  11. from reportlab.lib.pagesizes import A4
  12. from reportlab.pdfbase import pdfmetrics
  13. from reportlab.pdfbase.cidfonts import UnicodeCIDFont
  14. from reportlab.pdfgen import canvas
  15. def make_docx(path):
  16. doc = Document()
  17. doc.core_properties.title = "Quarterly Report"
  18. doc.add_heading("Quarterly Report", level=1)
  19. doc.add_paragraph("This report summarises revenue for the third quarter.")
  20. doc.add_heading("Revenue", level=2)
  21. p = doc.add_paragraph("Revenue grew by ")
  22. p.add_run("12 percent").bold = True
  23. p.add_run(" compared with the previous quarter.")
  24. table = doc.add_table(rows=3, cols=2)
  25. for r, (a, b) in enumerate([("Region", "Revenue"), ("North", "120"), ("South", "95")]):
  26. table.cell(r, 0).text = a
  27. table.cell(r, 1).text = b
  28. doc.add_heading("Outlook", level=2)
  29. doc.add_paragraph("We expect steady growth. 预计下季度保持稳定增长。")
  30. for i in range(1, 4):
  31. doc.add_paragraph(f"Risk item {i}: supply chain delays.", style="List Bullet")
  32. q = doc.add_paragraph("See ")
  33. q.add_run("https://example.com/report").font.size = Pt(10)
  34. doc.add_page_break()
  35. doc.add_heading("Appendix", level=2)
  36. doc.add_paragraph("Methodology notes live on the second page.")
  37. doc.save(path)
  38. def make_xlsx(path):
  39. wb = Workbook()
  40. ws = wb.active
  41. ws.title = "Revenue"
  42. ws.append(["Region", "Q1", "Q2", "Q3", "Total"])
  43. for cell in ws[1]:
  44. cell.font = Font(bold=True)
  45. cell.fill = PatternFill("solid", fgColor="DDEBF7")
  46. rows = [("North", 100, 110, 120), ("South", 80, 90, 95), ("East", 60, 65, 70)]
  47. for i, (region, *values) in enumerate(rows, start=2):
  48. ws.append([region, *values, f"=SUM(B{i}:D{i})"])
  49. ws.append(["Total", "=SUM(B2:B4)", "=SUM(C2:C4)", "=SUM(D2:D4)", "=SUM(E2:E4)"])
  50. ws.merge_cells("A7:E7")
  51. ws["A7"] = "Figures in thousands. 单位:千元"
  52. notes = wb.create_sheet("Notes")
  53. notes["A1"] = "Assumption"
  54. notes["B1"] = "Value"
  55. notes["A2"] = "Growth"
  56. notes["B2"] = 0.12
  57. notes["B2"].number_format = "0%"
  58. wb.save(path)
  59. def make_pptx(path):
  60. prs = Presentation()
  61. s1 = prs.slides.add_slide(prs.slide_layouts[0])
  62. s1.shapes.title.text = "Product Launch"
  63. s1.placeholders[1].text = "Plan for the autumn release"
  64. s2 = prs.slides.add_slide(prs.slide_layouts[1])
  65. s2.shapes.title.text = "Timeline"
  66. body = s2.placeholders[1].text_frame
  67. body.text = "September: beta"
  68. body.add_paragraph().text = "October: launch"
  69. body.add_paragraph().text = "十一月:复盘"
  70. box = s2.shapes.add_textbox(Inches(6), Inches(5.5), Inches(3), Inches(1))
  71. box.text_frame.text = "Owner: Dana"
  72. box.text_frame.paragraphs[0].runs[0].font.size = PptPt(14)
  73. s2.notes_slide.notes_text_frame.text = "Mention the beta feedback numbers."
  74. s3 = prs.slides.add_slide(prs.slide_layouts[5])
  75. s3.shapes.title.text = "Budget"
  76. rows, cols = 3, 3
  77. table = s3.shapes.add_table(rows, cols, Inches(1), Inches(2), Inches(8), Inches(1.5)).table
  78. for r, row in enumerate([("Item", "Cost", "Owner"), ("Ads", "40k", "Lee"), ("Events", "25k", "Kim")]):
  79. for c, value in enumerate(row):
  80. table.cell(r, c).text = value
  81. prs.save(path)
  82. def make_pdf(path):
  83. pdfmetrics.registerFont(UnicodeCIDFont("STSong-Light"))
  84. c = canvas.Canvas(path, pagesize=A4)
  85. c.setTitle("Research Paper")
  86. width, height = A4
  87. c.setFont("Helvetica-Bold", 20)
  88. c.drawString(72, height - 90, "Research Paper")
  89. c.setFont("Helvetica", 12)
  90. lines = [
  91. "Abstract: we study the effect of caching on latency.",
  92. "Results show a 40 percent reduction in p95 latency.",
  93. ]
  94. for i, line in enumerate(lines):
  95. c.drawString(72, height - 130 - i * 18, line)
  96. c.setFont("STSong-Light", 12)
  97. c.drawString(72, height - 180, "中文摘要:缓存显著降低延迟。")
  98. c.linkURL("https://example.com/paper", (72, height - 220, 300, height - 200))
  99. c.setFont("Helvetica", 12)
  100. c.drawString(72, height - 215, "https://example.com/paper")
  101. c.bookmarkPage("intro")
  102. c.addOutlineEntry("Introduction", "intro", level=0)
  103. c.showPage()
  104. c.setFont("Helvetica-Bold", 16)
  105. c.drawString(72, height - 90, "Method")
  106. c.setFont("Helvetica", 12)
  107. c.drawString(72, height - 120, "We replay production traces against three cache sizes.")
  108. c.bookmarkPage("method")
  109. c.addOutlineEntry("Method", "method", level=0)
  110. c.showPage()
  111. c.save()
  112. if __name__ == "__main__":
  113. make_docx("report.docx")
  114. make_xlsx("budget.xlsx")
  115. make_pptx("launch.pptx")
  116. make_pdf("paper.pdf")