Here我發現這個代碼拆分pdf頁面。pyPdf如何理解文檔邊界?
#!/usr/bin/env python
import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()
for p in [input.getPage(i) for i in range(0,input.getNumPages())]:
q = copy.copy(p)
(w, h) = p.mediaBox.upperRight
p.mediaBox.upperRight = (w/2, h)
q.mediaBox.upperLeft = (w/2, h)
output.addPage(p)
output.addPage(q)
output.write(sys.stdout)
如果一個頁面包含四個另一頁是這樣的:
+-------+-------+
| 1 | 2 |
|-------+-------|
| 3 | 4 |
+-------+-------+
然後代碼將其分割到兩頁(按照這個順序)含有另外兩個頁面:
+-------+-------+
| 3 | 4 |
+-------+-------+
+-------+-------+
| 1 | 2 |
+-------+-------+
你可以測試它,例如在following文件上。如果我理解正確upperRight
,upperLeft
(等)在代碼中提到的變量,那麼這是所看到pyPdf文檔表示:
UL(0,10) UR(10,10)
+-------+-------+
| 1 | 2 |
|-------+-------|
| 3 | 4 |
+-------+-------+
LL(0,0) LR(10,0)
UL(x,y) = UpperLeft
UR(x,y) = UpperRight
LL(x,y) = LowerLeft
LR(x,y) = LowerRight
據提到代碼:
(w, h) = p.mediaBox.upperRight
p.mediaBox.upperRight = (w/2, h)
q.mediaBox.upperLeft = (w/2, h)
我期待這個輸出:
p:
+-------+
| 1 |
|-------+
| 3 |
+-------+
q:
+-------+
| 2 |
|-------+
| 4 |
+-------+
我在這裏失蹤了什麼?
你得到了什麼,而不是你所期望的? – usr2564301
它在後文中描述,請參見圖2.我期待垂直分割,但是相反,我會以相反的順序進行水平分割。 –