473,472 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

split pdf chapters

2 New Member
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 6575
pdmorete
2 New Member
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
WhiteRider
37 New Member
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.