Using Python to parse pdf and extract Author and Book name
<p>I have a Mailing Reference List in a form a pdf. The mailing list has a very general format i.e Author Name followed by the Name of the book.
Consider the following examples:</p>

<p><strong>American Reading List</strong></p>

<p><strong>Democratic Theory</strong></p>

<p>â¢ Dahl, Preface to Democratic Theory</p>

<p>â¢ Schumpeter, Capitalism, Socialism, and Democracy (Introduction and part IV only)</p>

<p>â¢ Machperson, Life and Times of Liberal Democracy</p>

<p>â¢ Dahl, Democracy and its Critics</p>

<p>Now I am trying to parse the pdf using pdf miner and create a list where in the first index is the author name and the second index is the name of the book just like this:</p>

<p>[Dahl, Preface to Democratic Theory]</p>

<p>I am trying to use the split functionality because there is a comma and a space followed by the Author name. However I don't get the correct results.
Can somebody help?</p>

<pre><code>def extract():
    string = convert_pdf_to_txt("/Users/../../names.pdf")
    lines = list(filter(bool, string.split('\n')))
    for i in lines:
        check.extend(i.split(','))
    x=remove_numbers(check)
    remove_blank= [x for x in x if x]
    combine_two = [remove_blank[x:x + 2] for x in xrange(0,len(remove_blank), 2)]
    print combine_two
</code></pre>

<p>Let's see what's going wrong here. I'm making some guesses, but hopefully they are the relevant ones.</p>

<ol>
<li>Your <code>convert_pdf_to_text()</code> function returns a single long string containing all the text of the PDF.</li>
<li>You split the text on <code>", "</code> which results in a list of strings.</li>
</ol>

<p>Given your example data, this list looks something like this (each element is on a separate line here):</p>

<pre><code>Dahl
Preface to Democratic Theory(line break)(bullet)(tab)Schumpeter
Captitalism
Socialism
and Democracy (Introduction and part IV only)(line break)(bullet)(tab)Machpherson
Life and Times of Liberal Democracy(line break)(bullet)(tab)Dahl
Democracy and its Critics
</code></pre>

<p>Because you split on <code>", "</code> without regard for the fact that the data is formatted as lines, you end up with stuff from multiple lines in each item.</p>

<ol start="3">
<li>Now you use <code>filter()</code> to iterate over this list and filter out all the ones that aren't true. A non-empty string is true, and all of the elements are non-empty strings, so all the elements get through. Your <code>filter()</code> therefore doesn't do anything. </li>
</ol>

<p>What you seem to want is something more like this:</p>

<pre><code>lines = [line.split(", ", 1) for line in string.splitlines() if ", " in line]
</code></pre>

<p>Here we first split the lines, filter out any that don't have comma-space in them, and return a list of lists based on splitting the string on the first comma-space.</p>

python pdf split