472,958 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

split pdf chapters

Hello,
I am trying to split pdf chapters from a series of pdf files.
I am using pyPdf library.
The page range to extract comes from a text file.
My code works fine for just one file but I don't know is how to specify variable input and output filenames.
Sorry if my question is stupid but I am new to programming.
Here is the code. Thank you for any help.

# import pyPdf and open the input file
from pyPdf import PdfFileWriter, PdfFileReader
output = PdfFileWriter()
input1 = PdfFileReader(file("EEAL1977.pdf", "rb"))

#process the text file with the page rangs
fh = open('pages.txt')
for line in fh:
line = line.strip()
a,z = [int(x) for x in line.split()]
print a,z

#add pages to the output file
for n in range (a, z):
output.addPage(input1.getPage(n))

#write the file and close
outputStream = file("document.pdf", "wb")
output.write(outputStream)
outputStream.close()
Sep 27 '07 #1
2 6480
Well, I guess nobody was interested in the subject, so I answer myself, just in the case someone needs to use this in the future. The following is the correct code.
Now I only need to make it save with filenames that automatically increase by one. For now I just type the name for each extract I save

Expand|Select|Wrap|Line Numbers
  1. from pyPdf import PdfFileWriter, PdfFileReader
  2.  
  3. fh = open('pages.txt')
  4. for line in fh:
  5.                 linea = line.strip()
  6.                 a,z = (int(x) for x in linea.split())
  7.                 print a,z
  8.                 print 'otro'
  9.  
  10.                 output = PdfFileWriter()
  11.                 input1 = PdfFileReader(file("EEAL1977.pdf", "rb"))
  12.                 for e in range (a,z):
  13.                     output.addPage(input1.getPage(e))
  14.  
  15. #        x = output.addPage(input1.getPage(n))
  16. #        a = 1+a
  17. #        z = 'document' + str(a) + '.pdf'
  18.  
  19.  
  20.                 outputStream = file(raw_input("save as: "), "wb")
  21.                 output.write(outputStream)
  22.                 outputStream.close()
  23.                 outputStream = None
  24.  
Oct 1 '07 #2
Well, I guess nobody was interested in the subject, so I answer myself, just in the case someone needs to use this in the future. The following is the correct code.
Now I only need to make it save with filenames that automatically increase by one. For now I just type the name for each extract I save

Expand|Select|Wrap|Line Numbers
  1. from pyPdf import PdfFileWriter, PdfFileReader
  2.  
  3. fh = open('pages.txt')
  4. for line in fh:
  5.                 linea = line.strip()
  6.                 a,z = (int(x) for x in linea.split())
  7.                 print a,z
  8.                 print 'otro'
  9.  
  10.                 output = PdfFileWriter()
  11.                 input1 = PdfFileReader(file("EEAL1977.pdf", "rb"))
  12.                 for e in range (a,z):
  13.                     output.addPage(input1.getPage(e))
  14.  
  15. #        x = output.addPage(input1.getPage(n))
  16. #        a = 1+a
  17. #        z = 'document' + str(a) + '.pdf'
  18.  
  19.  
  20.                 outputStream = file(raw_input("save as: "), "wb")
  21.                 output.write(outputStream)
  22.                 outputStream.close()
  23.                 outputStream = None
  24.  
Did you add this module? I don't seem to have it as a default. If you did download it separately then thats probably why you didn't get any help - since theres not many people with pyPdf experience. Be aware that this code won't work on other computers with just a standard install of Python. Each computer will need this extra module installed too.

Anyway I'm glad you found a solution =)
Oct 1 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: Will McGugan | last post by:
Hi, I'm curious about the behaviour of the str.split() when applied to empty strings. "".split() returns an empty list, however.. "".split("*") returns a list containing one empty string. ...
6
by: Andreas Prilop | last post by:
What exactly are Chapters and Sections in the <LINK REL=...> tag? I have put some <LINK REL="Chapter"> tags into http://www.unics.uni-hannover.de/nhtcapri/bidirectional-text.html Is this the...
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
3
by: parag_paul | last post by:
http://concentratedlemonjuice.blogspot.com/2008/06/about-c-puzzles-on-my-blog.html Try this one for 30 chapters on C++ doubts,
1
by: =?Utf-8?B?amFtZXNjaGk=?= | last post by:
I'm trying to figure out how to programatically set the dvd chapter using the AxWindowsMediaPlayer ocx control in a C# project with no avail. My goal is to capture the in and out point of a dvd,...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.