473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 6567
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.