这里我找到了这个分割pdf页面的代码。
#!/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 |
+-------+-------+您可以在下面是文档上进行测试。如果我正确地理解了代码中提到的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 |
+-------+我在这里错过了什么?
发布于 2015-11-15 11:39:51
在PDF中,有两种获取景观页面的方法:
您的PDF示例使用了第二种方法:所有页面都是595x842,旋转270度。不考虑旋转会导致垂直解释为水平的,反之亦然。
https://stackoverflow.com/questions/33714894
复制相似问题