mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
The documents that showed how Word lays out pages, and the PDFs Word saved of them, were only in a git-ignored build folder. They are now committed in scripts/layout-probes, with the .docx Word opened for each PDF and the scripts that read them, and a README that says what each probe settles, how to make its .docx, lay it out in LibreOffice and read Word's PDF. The scripts import from docx, as the demos do, and write their .docx to build/word-probes.
13 lines
788 B
Python
13 lines
788 B
Python
# Prints the lines of one page of pdftotext -bbox-layout HTML, with their top, bottom and height in points, the gap from
|
|
# the line above in twips, and their left edge.
|
|
# Usage: python3 scripts/layout-probes/pagelines.py build/word-probes/word-tables.html 1
|
|
import html, re, sys
|
|
text = open(sys.argv[1]).read()
|
|
page = re.findall(r"<page.*?</page>", text, re.S)[int(sys.argv[2]) - 1]
|
|
prev = None
|
|
for line in re.findall(r'<line xMin="([\d.]+)" yMin="([\d.]+)" xMax="[\d.]+" yMax="([\d.]+)">(.*?)</line>', page, re.S):
|
|
words = " ".join(html.unescape(w) for w in re.findall(r">([^<]*)</word>", line[3]))
|
|
y = float(line[1])
|
|
print(f"{y:7.2f} {float(line[2]):7.2f} h{float(line[2]) - y:5.2f} d{(y - prev) * 20 if prev else 0:6.1f} x{float(line[0]):6.1f} {words[:60]}")
|
|
prev = y
|