def test_attr_access(resources): pdf = Pdf.open(resources / 'graph.pdf') assert int(pdf.root.Pages.Count) == 1
def test_empty_pdf(outdir): q = Pdf.new() with pytest.raises(IndexError): q.pages[0] q.save(outdir / 'empty.pdf')
def pal(resources): return Pdf.open(resources / 'pal-1bit-rgb.pdf')
import split_pdf from pikepdf import Pdf if __name__ == '__main__': data = {} file_path = input("请输入文件路径: ") opcode = int(input("清输入操作码: ")) if opcode == 0: # 提取单个页面 data["number"] = int(input("请输入要获得的页: ")) if opcode == 1: # 提取部分文件 data["start"] = int(input("请输入起始页: ")) try: data["end"] = int(input("请输入终点页: ")) except Exception as invalid_input_error: data["end"] = None stream = Pdf.open(file_path) split_pdf.split_pdf(stream, opcode, data)
def fourpages(resources): return Pdf.open(resources / 'fourpages.pdf')
def graph(resources): # Has XMP and docinfo, all standard format XMP with Pdf.open(resources / 'graph.pdf') as pdf: yield pdf
def trivial(resources): # Has no XMP or docinfo with Pdf.open(resources / 'pal-1bit-trivial.pdf') as pdf: yield pdf
def test_memory(self, resources): pdf = (resources / 'pal-1bit-trivial.pdf').read_bytes() with pytest.raises(Exception): pdf = Pdf.open(pdf)
def trivial(resources): return Pdf.open(resources / 'pal-1bit-trivial.pdf', access_mode=pikepdf.AccessMode.mmap)
def test_no_text_stream(self, resources): with pytest.raises(TypeError): with (resources / 'pal-1bit-trivial.pdf').open('r') as stream: Pdf.open(stream)
def test_read_not_readable_file(self, outdir): writable = (Path(outdir) / 'writeme.pdf').open('wb') with pytest.raises(ValueError, match=r'not readable'): Pdf.open(writable)
def test_stream(self, resources): with (resources / 'pal-1bit-trivial.pdf').open('rb') as stream: pdf = Pdf.open(stream) assert pdf.root.Pages.Count == 1
def test_overwrite_input(resources, outdir): copy(resources / 'sandwich.pdf', outdir / 'sandwich.pdf') p = Pdf.open(outdir / 'sandwich.pdf') with pytest.raises(ValueError, match=r'overwrite input file'): p.save(outdir / 'sandwich.pdf')
def test_overwrite_with_memory_file(outdir): (outdir / 'example.pdf').touch() pdf = Pdf.new() pdf.save(outdir / 'example.pdf')
def test_some_permissions_missing(self, resources): pdf = Pdf.open(resources / 'graph-encrypted.pdf', 'owner') assert pdf.allow.print_highres == pdf.allow.modify_annotation == False
def test_non_filename(): with pytest.raises(TypeError): Pdf.open(42.0)
def vera(resources): # Has XMP but no docinfo with Pdf.open(resources / 'veraPDF test suite 6-2-10-t02-pass-a.pdf') as pdf: yield pdf
def test_file_descriptor(resources): with (resources / 'pal-1bit-trivial.pdf').open('rb') as f: with pytest.raises(TypeError): Pdf.open(f.fileno())
def sandwich(resources): # Has XMP, docinfo, <?adobe-xap-filters esc="CRLF"?>, shorthand attribute XMP with Pdf.open(resources / 'sandwich.pdf') as pdf: yield pdf
def test_not_existing_file(): with pytest.raises(FileNotFoundError): Pdf.open('does_not_exist.pdf')
def invalid_creationdate(resources): # Has nuls in docinfo, old PDF with Pdf.open(resources / 'invalid_creationdate.pdf') as pdf: yield pdf
def test_empty(outdir): target = outdir / 'empty.pdf' target.touch() with pytest.raises(PdfError): Pdf.open(target)
def graph(resources): return Pdf.open(resources / 'graph.pdf')
def test_open_pdf_wrong_password(self, resources): # The correct passwords are "owner" and "user" with pytest.raises(PasswordError): Pdf.open(resources / 'graph-encrypted.pdf', password='******')
def sandwich(resources): return Pdf.open(resources / 'sandwich.pdf')
def test_open_pdf_password_encoding(self, resources): with pytest.raises(PasswordError): Pdf.open(resources / 'graph-encrypted.pdf', password=b'\x01\xfe')
def outlines_doc(resources): # Contains an outline with references return Pdf.open(resources / 'outlines.pdf')
def test_open_pdf_no_password_but_needed(self, resources): with pytest.raises(PasswordError): Pdf.open(resources / 'graph-encrypted.pdf')
def inline(resources): pdf = Pdf.open(resources / 'image-mono-inline.pdf') for operands, _command in parse_content_stream(pdf.pages[0]): if operands and isinstance(operands[0], PdfInlineImage): return operands[0], pdf
def test_open_pdf_password(resources): pdf = Pdf.open(resources / 'graph-encrypted.pdf', password='******') assert pdf.root['/Pages']['/Count'] == 1