Couple more PDF utilities

This commit is contained in:
Kovid Goyal 2019-07-17 09:37:13 +05:30
parent 4bf5b9c755
commit 19ca8d8bd8
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 26 additions and 25 deletions

View File

@ -10,7 +10,7 @@ from calibre.constants import plugins, preferred_encoding
from calibre.ebooks.metadata import authors_to_string
from calibre.ptempfile import TemporaryDirectory
from calibre.utils.ipc.simple_worker import WorkerError, fork_job
from polyglot.builtins import range, unicode_type, iteritems
from polyglot.builtins import unicode_type, iteritems
def get_podofo():
@ -106,24 +106,6 @@ def set_metadata_(tdir, title, authors, bkp, tags, xmp_packet):
return touched
def delete_all_but(path, pages):
''' Delete all the pages in the pdf except for the specified ones. Negative
numbers are counted from the end of the PDF. '''
podofo = get_podofo()
p = podofo.PDFDoc()
with open(path, 'rb') as f:
raw = f.read()
p.load(raw)
total = p.page_count()
pages = {total + x if x < 0 else x for x in pages}
for page in range(total-1, -1, -1):
if page not in pages:
p.delete_page(page)
with open(path, 'wb') as f:
p.save_to_fileobj(f)
def get_xmp_metadata(path):
podofo = get_podofo()
p = podofo.PDFDoc()

View File

@ -183,11 +183,11 @@ PDFDoc_image_count(PDFDoc *self, PyObject *args) {
// delete_page() {{{
static PyObject *
PDFDoc_delete_page(PDFDoc *self, PyObject *args) {
int num = 0;
if (PyArg_ParseTuple(args, "i", &num)) {
PDFDoc_delete_pages(PDFDoc *self, PyObject *args) {
int page = 0, count = 1;
if (PyArg_ParseTuple(args, "i|i", &page, &count)) {
try {
self->doc->DeletePages(num, 1);
self->doc->DeletePages(page - 1, count);
} catch(const PdfError & err) {
podofo_set_exception(err);
return NULL;
@ -197,6 +197,22 @@ PDFDoc_delete_page(PDFDoc *self, PyObject *args) {
Py_RETURN_NONE;
} // }}}
// copy_page() {{{
static PyObject *
PDFDoc_copy_page(PDFDoc *self, PyObject *args) {
int from = 0, to = 0;
if (!PyArg_ParseTuple(args, "ii", &from, &to)) return NULL;
try {
PdfPagesTree* tree = self->doc->GetPagesTree();
PdfPage* page = tree->GetPage(from - 1);
tree->InsertPage(to - 1, page);
} catch(const PdfError & err) {
podofo_set_exception(err);
return NULL;
}
Py_RETURN_NONE;
} // }}}
// append() {{{
static PyObject *
PDFDoc_append(PDFDoc *self, PyObject *args) {
@ -722,8 +738,11 @@ static PyMethodDef PDFDoc_methods[] = {
{"remove_fonts", (PyCFunction)remove_fonts, METH_VARARGS,
"remove_fonts() -> Remove the specified font objects."
},
{"delete_page", (PyCFunction)PDFDoc_delete_page, METH_VARARGS,
"delete_page(page_num) -> Delete the specified page from the pdf (0 is the first page)."
{"delete_pages", (PyCFunction)PDFDoc_delete_pages, METH_VARARGS,
"delete_page(page_num, count=1) -> Delete the specified pages from the pdf."
},
{"copy_page", (PyCFunction)PDFDoc_copy_page, METH_VARARGS,
"copy_page(from, to) -> Copy the specified page."
},
{"append", (PyCFunction)PDFDoc_append, METH_VARARGS,
"append(doc) -> Append doc (which must be a PDFDoc) to this document."