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()
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 -
from pyPdf import PdfFileWriter, PdfFileReader
-
-
fh = open('pages.txt')
-
for line in fh:
-
linea = line.strip()
-
a,z = (int(x) for x in linea.split())
-
print a,z
-
print 'otro'
-
-
output = PdfFileWriter()
-
input1 = PdfFileReader(file("EEAL1977.pdf", "rb"))
-
for e in range (a,z):
-
output.addPage(input1.getPage(e))
-
-
# x = output.addPage(input1.getPage(n))
-
# a = 1+a
-
# z = 'document' + str(a) + '.pdf'
-
-
-
outputStream = file(raw_input("save as: "), "wb")
-
output.write(outputStream)
-
outputStream.close()
-
outputStream = None
-
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 -
from pyPdf import PdfFileWriter, PdfFileReader
-
-
fh = open('pages.txt')
-
for line in fh:
-
linea = line.strip()
-
a,z = (int(x) for x in linea.split())
-
print a,z
-
print 'otro'
-
-
output = PdfFileWriter()
-
input1 = PdfFileReader(file("EEAL1977.pdf", "rb"))
-
for e in range (a,z):
-
output.addPage(input1.getPage(e))
-
-
# x = output.addPage(input1.getPage(n))
-
# a = 1+a
-
# z = 'document' + str(a) + '.pdf'
-
-
-
outputStream = file(raw_input("save as: "), "wb")
-
output.write(outputStream)
-
outputStream.close()
-
outputStream = None
-
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 =)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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.
...
|
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...
|
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"
|
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,...
|
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......
|
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() =...
|
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,
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |