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 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 -
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: 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=()=>{
|
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...
|
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 :...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |