[{"data":1,"prerenderedAt":121},["Reactive",2],{"/guides/python/aiohttp/exporting-only-a-specific-set-of-pages":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":5,"property":11,"output":12,"default":6,"body":13,"_type":116,"_id":117,"_source":118,"_file":119,"_extension":120},"/guides/python/aiohttp/exporting-only-a-specific-set-of-pages","aiohttp",false,"","Exporting only a specific set of pages","Learn how to export only a specific set of pages from a document using Python and the aiohttp library. This guide offers detailed steps with code samples in Python and the aiohttp library, highlighting how you can export only a specific set of pages from a document using PDFShift's API.","Python","pages","pdf",{"type":14,"children":15,"toc":113},"root",[16,24,37,42,47,91,96,101],{"type":17,"tag":18,"props":19,"children":20},"element","p",{},[21],{"type":22,"value":23},"text","In this guide, we'll show you how to export only a specific set of pages from a document using Python and the aiohttp library to convert them to PDF using PDFShift's API.",{"type":17,"tag":18,"props":25,"children":26},{},[27,29,35],{"type":22,"value":28},"When you're converting a document, you might want to export only a specific set of pages from a document. This can be done by setting the ",{"type":17,"tag":30,"props":31,"children":33},"code",{"className":32},[],[34],{"type":22,"value":11},{"type":22,"value":36}," parameter to the request.",{"type":17,"tag":18,"props":38,"children":39},{},[40],{"type":22,"value":41},"It behaves exactly like the page parameter you can see when you print a document from your browser.",{"type":17,"tag":18,"props":43,"children":44},{},[45],{"type":22,"value":46},"It accepts the following pattern:",{"type":17,"tag":48,"props":49,"children":50},"ul",{},[51,65,78],{"type":17,"tag":52,"props":53,"children":54},"li",{},[55,57,63],{"type":22,"value":56},"A number, such as ",{"type":17,"tag":30,"props":58,"children":60},{"className":59},[],[61],{"type":22,"value":62},"2",{"type":22,"value":64},". This will print the page 2",{"type":17,"tag":52,"props":66,"children":67},{},[68,70,76],{"type":22,"value":69},"A range, such as ",{"type":17,"tag":30,"props":71,"children":73},{"className":72},[],[74],{"type":22,"value":75},"2-4",{"type":22,"value":77},". This will print the pages 2 to 4 (2, 3 and 4)",{"type":17,"tag":52,"props":79,"children":80},{},[81,83,89],{"type":22,"value":82},"A list, such as ",{"type":17,"tag":30,"props":84,"children":86},{"className":85},[],[87],{"type":22,"value":88},"2,4,5,9",{"type":22,"value":90},". This will print the pages 2, 4, 5 and 9",{"type":17,"tag":18,"props":92,"children":93},{},[94],{"type":22,"value":95},"This way, you can only export the pages that you really need and get immediately the result you want in the PDF, without having to edit it.",{"type":17,"tag":18,"props":97,"children":98},{},[99],{"type":22,"value":100},"Here's an example:",{"type":17,"tag":102,"props":103,"children":108},"pre",{"className":104,"code":106,"language":107,"meta":7},[105],"language-python","import aiohttp, asyncio, json\n\n# You can get an API key at https://pdfshift.io\napi_key = 'sk_xxxxxxxxxxxx'\n\nparams = {\n    'source': 'https://en.wikipedia.org/wiki/PDF',\n    'pages': '2-4'\n}\n\nresponse = None\ntry:\n    async with aiohttp.ClientSession() as session:\n        async with session.post(\n            'https://api.pdfshift.io/v3/convert/pdf',\n            headers={'X-API-Key': api_key},\n            json=params\n        ) as response:\n            if response.status >= 400:\n                raise Exception('Invalid request: {}'.format(await response.text()))\n\n            with open('result.pdf', 'wb') as f:\n                f.write(await response.read())\n\n            print('The PDF document was generated and saved to result.pdf')\nexcept asyncio.TimeoutError:\n    raise Exception('The request took too long to process')\nexcept aiohttp.ClientError as e:\n    raise Exception(f'An error occurred: {e}')\nexcept Exception as e:\n    # We highly recommend you to handle exceptions. Often, PDFShift will provide you with a clear explanation about what happened.\n    # Moreover, in case of error, no PDF are returned !\n    raise Exception(f'An error occurred: {e}')\n","python",[109],{"type":17,"tag":30,"props":110,"children":111},{"__ignoreMap":7},[112],{"type":22,"value":106},{"title":7,"searchDepth":114,"depth":114,"links":115},2,[],"markdown","content:guides:python:aiohttp:exporting-only-a-specific-set-of-pages.md","content","guides/python/aiohttp/exporting-only-a-specific-set-of-pages.md","md",1785426833182]