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
| import random from docx import Document from docx.shared import Pt, Cm, Inches, RGBColor from docx.enum.text import WD_ALIGN_PARAGRAPH from docx.enum.table import WD_TABLE_ALIGNMENT from docx.enum.section import WD_ORIENT from docx.oxml.ns import qn, nsdecls from docx.oxml import parse_xml
def set_cell_shading(cell, color): """设置单元格背景色""" shading_elm = parse_xml( f'<w:shd xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:fill="{color}"/>' ) cell._tc.get_or_add_tcPr().append(shading_elm)
def set_cell_border(cell, **kwargs): """设置单元格边框""" tc = cell._tc tcPr = tc.get_or_add_tcPr() tcBorders = parse_xml( f'<w:tcBorders xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:val="single" w:sz="0" w:color="auto"/>' ) tcPr.append(tcBorders)
def add_int_addition_negative(): """整数加法(含负数)""" a = random.randint(-20, 20) b = random.randint(-20, 20) return f"{a} + ({b}) = "
def add_int_subtraction_negative(): """整数减法(含负数)""" a = random.randint(-20, 20) b = random.randint(-20, 20) return f"{a} - ({b}) = "
def add_int_multiplication_negative(): """整数乘法(含负数)""" a = random.randint(-10, 10) b = random.randint(-10, 10) return f"{a} × {b} = "
def add_int_division_negative(): """整数除法(含负数)""" while True: b = random.randint(-10, 10) if b == 0: continue result = random.randint(-10, 10) a = b * result if a < -100 or a > 100: continue return f"{a} ÷ {b} = "
def add_decimal_addition(): """小数加法""" a = round(random.uniform(-10, 10), 2) b = round(random.uniform(-10, 10), 2) return f"{a} + {b} = "
def add_decimal_subtraction(): """小数减法""" a = round(random.uniform(-10, 10), 2) b = round(random.uniform(-10, 10), 2) return f"{a} - {b} = "
def add_decimal_multiplication(): """小数乘法""" a = round(random.uniform(-5, 5), 1) b = round(random.uniform(-5, 5), 1) return f"{a} × {b} = "
def add_decimal_division(): """小数除法""" while True: b = round(random.uniform(0.1, 5), 1) result = round(random.uniform(-5, 5), 1) a = round(b * result, 2) if a == 0: continue return f"{a} ÷ {b} = "
def add_fraction_addition(): """分数加法""" denominators = [2, 3, 4, 5, 6, 8, 10] d1 = random.choice(denominators) d2 = random.choice(denominators) n1 = random.randint(1, d1 - 1) n2 = random.randint(1, d2 - 1) sign1 = random.choice(['', '-']) sign2 = random.choice(['', '-']) return f"{sign1}{n1}/{d1} + {sign2}{n2}/{d2} = "
def add_fraction_subtraction(): """分数减法""" denominators = [2, 3, 4, 5, 6, 8, 10] d1 = random.choice(denominators) d2 = random.choice(denominators) n1 = random.randint(1, d1 - 1) n2 = random.randint(1, d2 - 1) sign1 = random.choice(['', '-']) sign2 = random.choice(['', '-']) return f"{sign1}{n1}/{d1} - {sign2}{n2}/{d2} = "
def add_fraction_multiplication(): """分数乘法""" denominators = [2, 3, 4, 5, 6] d1 = random.choice(denominators) d2 = random.choice(denominators) n1 = random.randint(1, d1 - 1) n2 = random.randint(1, d2 - 1) sign1 = random.choice(['', '-']) sign2 = random.choice(['', '-']) return f"{sign1}{n1}/{d1} × {sign2}{n2}/{d2} = "
def add_fraction_division(): """分数除法""" denominators = [2, 3, 4, 5, 6] d1 = random.choice(denominators) d2 = random.choice(denominators) n1 = random.randint(1, d1 - 1) n2 = random.randint(1, d2 - 1) sign1 = random.choice(['', '-']) sign2 = random.choice(['', '-']) return f"{sign1}{n1}/{d1} ÷ {sign2}{n2}/{d2} = "
def add_negative_number_simple(): """简单负数计算""" a = random.randint(-30, 30) b = random.randint(-30, 30) op = random.choice(['+', '-']) if op == '+': return f"({a}) + ({b}) = " else: return f"({a}) - ({b}) = "
def add_negative_abs(): """绝对值计算""" a = random.randint(-50, 50) return f"|{a}| = "
def add_find_x(): """求未知数x(简单方程)""" a = random.randint(-10, 10) result = random.randint(-20, 20) op = random.choice(['+', '-']) if op == '+': return f"x + ({a}) = {result},x = " else: return f"x - ({a}) = {result},x = "
def add_power_square(): """平方计算""" a = random.randint(-10, 10) return f"({a})² = "
def generate_problem_set(count=100): """生成题目集合""" generators = [ add_int_addition_negative, add_int_subtraction_negative, add_int_multiplication_negative, add_int_division_negative, add_decimal_addition, add_decimal_subtraction, add_decimal_multiplication, add_decimal_division, add_fraction_addition, add_fraction_subtraction, add_fraction_multiplication, add_fraction_division, add_negative_number_simple, add_negative_abs, add_find_x, add_power_square, ] problems = [] used = set() attempts = 0 while len(problems) < count and attempts < count * 50: gen = random.choice(generators) problem = gen() if problem not in used: used.add(problem) problems.append(problem) attempts += 1 return problems
def create_beautiful_paper(): """创建精美的口算卷子""" doc = Document() section = doc.sections[0] section.orientation = WD_ORIENT.LANDSCAPE section.page_width = Cm(29.7) section.page_height = Cm(21) section.top_margin = Cm(1.5) section.bottom_margin = Cm(1.5) section.left_margin = Cm(1.5) section.right_margin = Cm(1.5) title_para = doc.add_paragraph() title_para.alignment = WD_ALIGN_PARAGRAPH.CENTER title_run = title_para.add_run("六年级口算练习题(含负数)") title_run.font.size = Pt(22) title_run.font.bold = True title_run.font.name = '宋体' title_run.font.color.rgb = RGBColor(0x1A, 0x3C, 0x6E) deco_para = doc.add_paragraph() deco_para.alignment = WD_ALIGN_PARAGRAPH.CENTER deco_run = deco_para.add_run("━" * 60) deco_run.font.size = Pt(10) deco_run.font.color.rgb = RGBColor(0x4A, 0x90, 0xD9) info_para = doc.add_paragraph() info_para.alignment = WD_ALIGN_PARAGRAPH.CENTER info_table = doc.add_table(rows=1, cols=3) info_table.alignment = WD_TABLE_ALIGNMENT.CENTER cells = info_table.rows[0].cells labels = [ ("姓 名:", ""), ("班 级:", ""), ("得 分:", "") ] for i, (label, _) in enumerate(labels): cell = cells[i] cell.width = Cm(7) p = cell.paragraphs[0] p.alignment = WD_ALIGN_PARAGRAPH.CENTER run = p.add_run(label) run.font.size = Pt(14) run.font.bold = True run.font.name = '宋体' run.font.color.rgb = RGBColor(0x33, 0x33, 0x33) run2 = p.add_run("______") run2.font.size = Pt(14) run2.font.name = '宋体' run2.font.color.rgb = RGBColor(0x66, 0x66, 0x66) note_para = doc.add_paragraph() note_para.alignment = WD_ALIGN_PARAGRAPH.LEFT note_run = note_para.add_run("注意:请直接写出得数,不用写出计算过程。 时间:______ 分钟") note_run.font.size = Pt(11) note_run.font.name = '宋体' note_run.font.color.rgb = RGBColor(0x88, 0x88, 0x88) deco_para2 = doc.add_paragraph() deco_para2.alignment = WD_ALIGN_PARAGRAPH.CENTER deco_run2 = deco_para2.add_run("━" * 60) deco_run2.font.size = Pt(8) deco_run2.font.color.rgb = RGBColor(0x4A, 0x90, 0xD9) problems = generate_problem_set(100) cols = 4 rows_needed = (len(problems) + cols - 1) // cols table = doc.add_table(rows=rows_needed, cols=cols) table.alignment = WD_TABLE_ALIGNMENT.CENTER tbl = table._tbl tblPr = tbl.tblPr if tbl.tblPr is not None else parse_xml( f'<w:tblPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:tblW="0" w:tblType="auto"/>' ) tblBorders_xml = ( '<w:tblBorders xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">' '<w:top w:val="single" w:sz="4" w:color="4A90D9"/>' '<w:left w:val="single" w:sz="4" w:color="4A90D9"/>' '<w:bottom w:val="single" w:sz="4" w:color="4A90D9"/>' '<w:right w:val="single" w:sz="4" w:color="4A90D9"/>' '<w:insideH w:val="single" w:sz="4" w:color="4A90D9"/>' '<w:insideV w:val="single" w:sz="4" w:color="4A90D9"/>' '</w:tblBorders>' ) tblBorders = parse_xml(tblBorders_xml) tblPr.append(tblBorders) for idx, problem in enumerate(problems): row_idx = idx // cols col_idx = idx % cols cell = table.cell(row_idx, col_idx) cell.width = Cm(6.5) if row_idx % 2 == 0: set_cell_shading(cell, "E8F4FD") p = cell.paragraphs[0] p.alignment = WD_ALIGN_PARAGRAPH.LEFT p.paragraph_format.space_before = Pt(6) p.paragraph_format.space_after = Pt(6) p.paragraph_format.left_indent = Cm(0.3) num_run = p.add_run(f"{idx + 1:3d}. ") num_run.font.size = Pt(12) num_run.font.bold = True num_run.font.name = '宋体' num_run.font.color.rgb = RGBColor(0x1A, 0x3C, 0x6E) prob_run = p.add_run(problem) prob_run.font.size = Pt(12) prob_run.font.name = '宋体' prob_run.font.color.rgb = RGBColor(0x33, 0x33, 0x33) bottom_deco = doc.add_paragraph() bottom_deco.alignment = WD_ALIGN_PARAGRAPH.CENTER bottom_run = bottom_deco.add_run("━" * 60) bottom_run.font.size = Pt(8) bottom_run.font.color.rgb = RGBColor(0x4A, 0x90, 0xD9) output_path = "/Users/720coder/Documents/口算/六年级口算题_100道.docx" doc.save(output_path) print(f"文档已保存: {output_path}")
if __name__ == "__main__": random.seed() create_beautiful_paper()
|