Package: minecart
minecartis a Python package that simplifies the extraction of text,
images, and shapes from a PDF document. It provides a very Pythonic
interface to extract positioning, color, and font metadata for all of
the objects in the PDF. It is a pure-Python package (it depends onpdfminerfor the low-level parsing).minecarttakes
inspiration from Tim McNamara’sslate, but aims to provide more
detailed information:>>>pdffile=open('example.pdf','rb')>>>doc=minecart.Document(pdffile)>>>page=doc.get_page(3)>>>forshapeinpage.shapes.iter_in_bbox((0,0,100,200)):...printshape.path,shape.fill.color.as_rgb()>>>im=page.images[0].as_pil()# requires pillow>>>im.show()InstallationAs of version0.3.0, only Python 3 is support, usingpdfminer3k.The easy way:pip install minecartThe hard way: download the source code, change into the working
directory, and runpython setup.py installFor CJK languages: Supporting the CJK languages requires an
addtional step, asdetailedinpdfminer.FeaturesShapes: You can extract path information, bounding box, stroke
parameters, and stroke/fill colors. Color support is fairly robust,
allowing the simple.as_rgb()in most cases. (To be concrete,minecartsupports theDeviceRGB,DeviceCMYK,DeviceGray, andCIE-basedcolor spaces.Indexedcolors
are supported if they index into one of the above.)Images:minecartcan easily extract images toPIL.Imageobjects.Text: (CalledLetteringin the source) In addition to
extracting plain text from the PDF, you can access the
position/bounding box information and the font used.If there’s a feature you’d like to extract from a PDF that’s not
currently supported, open up an issue or submit a pull request! I’m
especially interested in hearing whether there are many PDFs using color
spaces outside of the ones currently supported.DocumentationThe main entry point will always beminecart.Document, which accepts
a single parameter, an open file-like object which will be read to
create the document. TheDocumenthas two primary methods for
accessing its contents:.get_page(num)and.iter_pages(). Both
methods returnminecart.Pageobjects, which provide access to the
graphical elements found on the page.Pageobjects have three main
attributes:.images: A list of all theminecart.Imageobjects found on
the page..letterings: A list of all the text objects found on the page, asLetteringobjects.Letteringis aunicodesubclass which
adds bounding box and font information (using.get_bbox()or.font)..shapes: A list of all the squares, circles, lines, etc. found on
the page asShapeobjects.Shapeobjects have three main
attributes of interest:.stroke: An object containing the stroke parameters used to
draw the shape..strokehas.color,.linewidth,.linecap,.linejoin,.miterlimit, and.dashattributes. If the shape was not stroked,.strokewill beNone..fill: An object containing the fill parameters used to draw
the shape. Right now,.fillonly has a.colorparameter..path: A list with the coordinates used to defined the shape,
as well as the type of line segment each set of coordinates
defines.  Refer to theminecart.Shapedocumentation for more
detailsNote on color: The PDF spec spends a fair amount of time dealing
with color specifications, defining color spaces, and transforms and
the like.minecart’s approach is to simplify things down with sensible
defaults, so that every color has an.as_rgb()method, which returns
a 3-tuple with component values between 0 (black) and 1 (white). If you
are interested in extracting colorspace families and parameters, you can
do that too, though!We try to keep docstrings complete and up to date, so you can read
through the source or usedirandhelpto see what methods are
available.SupportIf you are having trouble working withminecart, feel free to create
a new issue.ContributingBug reports are always welcome (using the GitHub tracker) as are feature
requests. The PDF spec has so many corners, it is hard to
prioritize which features to implement. If there’s
something you’d like to extract from a document but isn’t currently
supported, pleasecreate a new issue.If you’d like to contribute code, you can either create an issue and
include a patch (if the changes are small) or fork the project and
create a pull request.LicenseThis project is licensed under the MIT license.