"""统计 PDF 正文页的行距和每页行数,找页数偏差的来源。""" import statistics, sys, pdfplumber for path in sys.argv[1:]: pitches, counts, sizes, fonts = [], [], [], {} with pdfplumber.open(path) as pdf: for page in pdf.pages[5:25]: ys = sorted({round(c["top"], 1) for c in page.chars if c["text"].strip()}) if len(ys) < 10: continue counts.append(len(ys)) pitches += [b - a for a, b in zip(ys, ys[1:]) if 8 < b - a < 40] for c in page.chars: sizes.append(round(c["size"], 1)); fonts[c["fontname"].split("+")[-1]] = fonts.get(c["fontname"].split("+")[-1], 0) + 1 print(path.split("/out/")[-1], "lines/page", statistics.median(counts), "pitch", statistics.median(pitches), "size", statistics.mode(sizes), sorted(fonts.items(), key=lambda x: -x[1])[:3])