Center the page number in a footer using PyPDF
up vote
0
down vote
favorite
I'm using PyPDF to create a formatted report. I want the page number (e.g. Page 1 of 3) to be centered in the footer, pretty much exactly how the PyPDF tutorial shows. Here's the tutorial I'm referencing.
Below is the code I put in the footer method:
def footer(self):
genDateTime = "Report generated on: " + datetime.datetime.now().strftime('%m/%d/%Y %I:%M:%S %p')
page = 'Page ' + str(self.page_no()) + '/{nb}'
self.set_y(-10)
self.set_font('Arial', '', 9)
self.cell(0, 5, "Clinical Report: Confidential", 0, 0, 'L')
self.cell(0, 5, page, 0, 0, 'C')
self.cell(0, 5, genDateTime, 0, 0, 'R')
Here is a screenshot of the bottom of the page. As you can see, the Confidential and DateTime labels are showing up as expected, but the Page # label is right justified:
Thanks for the help.
python pdf pypdf
add a comment |
up vote
0
down vote
favorite
I'm using PyPDF to create a formatted report. I want the page number (e.g. Page 1 of 3) to be centered in the footer, pretty much exactly how the PyPDF tutorial shows. Here's the tutorial I'm referencing.
Below is the code I put in the footer method:
def footer(self):
genDateTime = "Report generated on: " + datetime.datetime.now().strftime('%m/%d/%Y %I:%M:%S %p')
page = 'Page ' + str(self.page_no()) + '/{nb}'
self.set_y(-10)
self.set_font('Arial', '', 9)
self.cell(0, 5, "Clinical Report: Confidential", 0, 0, 'L')
self.cell(0, 5, page, 0, 0, 'C')
self.cell(0, 5, genDateTime, 0, 0, 'R')
Here is a screenshot of the bottom of the page. As you can see, the Confidential and DateTime labels are showing up as expected, but the Page # label is right justified:
Thanks for the help.
python pdf pypdf
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm using PyPDF to create a formatted report. I want the page number (e.g. Page 1 of 3) to be centered in the footer, pretty much exactly how the PyPDF tutorial shows. Here's the tutorial I'm referencing.
Below is the code I put in the footer method:
def footer(self):
genDateTime = "Report generated on: " + datetime.datetime.now().strftime('%m/%d/%Y %I:%M:%S %p')
page = 'Page ' + str(self.page_no()) + '/{nb}'
self.set_y(-10)
self.set_font('Arial', '', 9)
self.cell(0, 5, "Clinical Report: Confidential", 0, 0, 'L')
self.cell(0, 5, page, 0, 0, 'C')
self.cell(0, 5, genDateTime, 0, 0, 'R')
Here is a screenshot of the bottom of the page. As you can see, the Confidential and DateTime labels are showing up as expected, but the Page # label is right justified:
Thanks for the help.
python pdf pypdf
I'm using PyPDF to create a formatted report. I want the page number (e.g. Page 1 of 3) to be centered in the footer, pretty much exactly how the PyPDF tutorial shows. Here's the tutorial I'm referencing.
Below is the code I put in the footer method:
def footer(self):
genDateTime = "Report generated on: " + datetime.datetime.now().strftime('%m/%d/%Y %I:%M:%S %p')
page = 'Page ' + str(self.page_no()) + '/{nb}'
self.set_y(-10)
self.set_font('Arial', '', 9)
self.cell(0, 5, "Clinical Report: Confidential", 0, 0, 'L')
self.cell(0, 5, page, 0, 0, 'C')
self.cell(0, 5, genDateTime, 0, 0, 'R')
Here is a screenshot of the bottom of the page. As you can see, the Confidential and DateTime labels are showing up as expected, but the Page # label is right justified:
Thanks for the help.
python pdf pypdf
python pdf pypdf
asked Nov 7 at 20:53
Kieran Paddock
691111
691111
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
I think the issue is with how you are setting cell width. PyPDF by default places cells sequentially, starting at the far right of the previously placed cell. When you call self.cell()
, the first argument is width -- by default if width is 0, it makes the width extend to the far right of the page. So when you place self.cell(0, 5, "Clinical Report: Confidential", 0, 0, 'L')
, that text box is extending all the way to the right of the page. Then when you try to place self.cell(0, 5, page, 0, 0, 'C')
, it is centering it in the remaining space on that line -- but there is no space left so it just places it at the end. Try giving your first cell some width like this:
def footer(self):
genDateTime = "Report generated on: " + datetime.datetime.now().strftime('%m/%d/%Y %I:%M:%S %p')
page = 'Page ' + str(self.page_no()) + '/{nb}'
self.set_y(-10)
self.set_font('Arial', '', 9)
self.cell(5, 5, "Clinical Report: Confidential", 0, 0, 'L')
self.cell(0, 5, page, 0, 0, 'C')
self.cell(0, 5, genDateTime, 0, 0, 'R')
You might have to make the width greater than 5 to display all your text, but you can play with that.
Worked perfect, thanks so much. Out of curiosity, why was the right justified time placing correctly if the left justified text took up the whole page width?
– Kieran Paddock
Nov 7 at 21:20
After you placed the first box (which took up the whole width), the centered box and right-justified box both were forced to be jammed into the end of the line. This isn't a problem for something that is right-justified (since it forces it all the way to the right anyway.) Does that make sense?
– killian95
Nov 7 at 21:49
@KieranPaddock did you delete your other question where I directed you to PyPDF?
– killian95
Nov 8 at 0:01
That makes sense, thanks. And I didn't delete it, I just looked at it and for some reason all the comments/answers are gone, though I didn't touch the post.
– Kieran Paddock
Nov 8 at 16:45
1
@KieranPaddock Ahh, ok. Looks like a mod deleted my answer -- presumably because it was only a link? That's why I couldn't find it in my history. However, I still think it was a useful question and answer pair so I reposted an answer with a simple demo, for what it's worth.
– killian95
Nov 8 at 17:51
add a comment |
up vote
0
down vote
You Could make your cell the same size as the page so that the
self.cell()
can center it to that size/orientation
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I think the issue is with how you are setting cell width. PyPDF by default places cells sequentially, starting at the far right of the previously placed cell. When you call self.cell()
, the first argument is width -- by default if width is 0, it makes the width extend to the far right of the page. So when you place self.cell(0, 5, "Clinical Report: Confidential", 0, 0, 'L')
, that text box is extending all the way to the right of the page. Then when you try to place self.cell(0, 5, page, 0, 0, 'C')
, it is centering it in the remaining space on that line -- but there is no space left so it just places it at the end. Try giving your first cell some width like this:
def footer(self):
genDateTime = "Report generated on: " + datetime.datetime.now().strftime('%m/%d/%Y %I:%M:%S %p')
page = 'Page ' + str(self.page_no()) + '/{nb}'
self.set_y(-10)
self.set_font('Arial', '', 9)
self.cell(5, 5, "Clinical Report: Confidential", 0, 0, 'L')
self.cell(0, 5, page, 0, 0, 'C')
self.cell(0, 5, genDateTime, 0, 0, 'R')
You might have to make the width greater than 5 to display all your text, but you can play with that.
Worked perfect, thanks so much. Out of curiosity, why was the right justified time placing correctly if the left justified text took up the whole page width?
– Kieran Paddock
Nov 7 at 21:20
After you placed the first box (which took up the whole width), the centered box and right-justified box both were forced to be jammed into the end of the line. This isn't a problem for something that is right-justified (since it forces it all the way to the right anyway.) Does that make sense?
– killian95
Nov 7 at 21:49
@KieranPaddock did you delete your other question where I directed you to PyPDF?
– killian95
Nov 8 at 0:01
That makes sense, thanks. And I didn't delete it, I just looked at it and for some reason all the comments/answers are gone, though I didn't touch the post.
– Kieran Paddock
Nov 8 at 16:45
1
@KieranPaddock Ahh, ok. Looks like a mod deleted my answer -- presumably because it was only a link? That's why I couldn't find it in my history. However, I still think it was a useful question and answer pair so I reposted an answer with a simple demo, for what it's worth.
– killian95
Nov 8 at 17:51
add a comment |
up vote
1
down vote
accepted
I think the issue is with how you are setting cell width. PyPDF by default places cells sequentially, starting at the far right of the previously placed cell. When you call self.cell()
, the first argument is width -- by default if width is 0, it makes the width extend to the far right of the page. So when you place self.cell(0, 5, "Clinical Report: Confidential", 0, 0, 'L')
, that text box is extending all the way to the right of the page. Then when you try to place self.cell(0, 5, page, 0, 0, 'C')
, it is centering it in the remaining space on that line -- but there is no space left so it just places it at the end. Try giving your first cell some width like this:
def footer(self):
genDateTime = "Report generated on: " + datetime.datetime.now().strftime('%m/%d/%Y %I:%M:%S %p')
page = 'Page ' + str(self.page_no()) + '/{nb}'
self.set_y(-10)
self.set_font('Arial', '', 9)
self.cell(5, 5, "Clinical Report: Confidential", 0, 0, 'L')
self.cell(0, 5, page, 0, 0, 'C')
self.cell(0, 5, genDateTime, 0, 0, 'R')
You might have to make the width greater than 5 to display all your text, but you can play with that.
Worked perfect, thanks so much. Out of curiosity, why was the right justified time placing correctly if the left justified text took up the whole page width?
– Kieran Paddock
Nov 7 at 21:20
After you placed the first box (which took up the whole width), the centered box and right-justified box both were forced to be jammed into the end of the line. This isn't a problem for something that is right-justified (since it forces it all the way to the right anyway.) Does that make sense?
– killian95
Nov 7 at 21:49
@KieranPaddock did you delete your other question where I directed you to PyPDF?
– killian95
Nov 8 at 0:01
That makes sense, thanks. And I didn't delete it, I just looked at it and for some reason all the comments/answers are gone, though I didn't touch the post.
– Kieran Paddock
Nov 8 at 16:45
1
@KieranPaddock Ahh, ok. Looks like a mod deleted my answer -- presumably because it was only a link? That's why I couldn't find it in my history. However, I still think it was a useful question and answer pair so I reposted an answer with a simple demo, for what it's worth.
– killian95
Nov 8 at 17:51
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I think the issue is with how you are setting cell width. PyPDF by default places cells sequentially, starting at the far right of the previously placed cell. When you call self.cell()
, the first argument is width -- by default if width is 0, it makes the width extend to the far right of the page. So when you place self.cell(0, 5, "Clinical Report: Confidential", 0, 0, 'L')
, that text box is extending all the way to the right of the page. Then when you try to place self.cell(0, 5, page, 0, 0, 'C')
, it is centering it in the remaining space on that line -- but there is no space left so it just places it at the end. Try giving your first cell some width like this:
def footer(self):
genDateTime = "Report generated on: " + datetime.datetime.now().strftime('%m/%d/%Y %I:%M:%S %p')
page = 'Page ' + str(self.page_no()) + '/{nb}'
self.set_y(-10)
self.set_font('Arial', '', 9)
self.cell(5, 5, "Clinical Report: Confidential", 0, 0, 'L')
self.cell(0, 5, page, 0, 0, 'C')
self.cell(0, 5, genDateTime, 0, 0, 'R')
You might have to make the width greater than 5 to display all your text, but you can play with that.
I think the issue is with how you are setting cell width. PyPDF by default places cells sequentially, starting at the far right of the previously placed cell. When you call self.cell()
, the first argument is width -- by default if width is 0, it makes the width extend to the far right of the page. So when you place self.cell(0, 5, "Clinical Report: Confidential", 0, 0, 'L')
, that text box is extending all the way to the right of the page. Then when you try to place self.cell(0, 5, page, 0, 0, 'C')
, it is centering it in the remaining space on that line -- but there is no space left so it just places it at the end. Try giving your first cell some width like this:
def footer(self):
genDateTime = "Report generated on: " + datetime.datetime.now().strftime('%m/%d/%Y %I:%M:%S %p')
page = 'Page ' + str(self.page_no()) + '/{nb}'
self.set_y(-10)
self.set_font('Arial', '', 9)
self.cell(5, 5, "Clinical Report: Confidential", 0, 0, 'L')
self.cell(0, 5, page, 0, 0, 'C')
self.cell(0, 5, genDateTime, 0, 0, 'R')
You might have to make the width greater than 5 to display all your text, but you can play with that.
answered Nov 7 at 21:08
killian95
60339
60339
Worked perfect, thanks so much. Out of curiosity, why was the right justified time placing correctly if the left justified text took up the whole page width?
– Kieran Paddock
Nov 7 at 21:20
After you placed the first box (which took up the whole width), the centered box and right-justified box both were forced to be jammed into the end of the line. This isn't a problem for something that is right-justified (since it forces it all the way to the right anyway.) Does that make sense?
– killian95
Nov 7 at 21:49
@KieranPaddock did you delete your other question where I directed you to PyPDF?
– killian95
Nov 8 at 0:01
That makes sense, thanks. And I didn't delete it, I just looked at it and for some reason all the comments/answers are gone, though I didn't touch the post.
– Kieran Paddock
Nov 8 at 16:45
1
@KieranPaddock Ahh, ok. Looks like a mod deleted my answer -- presumably because it was only a link? That's why I couldn't find it in my history. However, I still think it was a useful question and answer pair so I reposted an answer with a simple demo, for what it's worth.
– killian95
Nov 8 at 17:51
add a comment |
Worked perfect, thanks so much. Out of curiosity, why was the right justified time placing correctly if the left justified text took up the whole page width?
– Kieran Paddock
Nov 7 at 21:20
After you placed the first box (which took up the whole width), the centered box and right-justified box both were forced to be jammed into the end of the line. This isn't a problem for something that is right-justified (since it forces it all the way to the right anyway.) Does that make sense?
– killian95
Nov 7 at 21:49
@KieranPaddock did you delete your other question where I directed you to PyPDF?
– killian95
Nov 8 at 0:01
That makes sense, thanks. And I didn't delete it, I just looked at it and for some reason all the comments/answers are gone, though I didn't touch the post.
– Kieran Paddock
Nov 8 at 16:45
1
@KieranPaddock Ahh, ok. Looks like a mod deleted my answer -- presumably because it was only a link? That's why I couldn't find it in my history. However, I still think it was a useful question and answer pair so I reposted an answer with a simple demo, for what it's worth.
– killian95
Nov 8 at 17:51
Worked perfect, thanks so much. Out of curiosity, why was the right justified time placing correctly if the left justified text took up the whole page width?
– Kieran Paddock
Nov 7 at 21:20
Worked perfect, thanks so much. Out of curiosity, why was the right justified time placing correctly if the left justified text took up the whole page width?
– Kieran Paddock
Nov 7 at 21:20
After you placed the first box (which took up the whole width), the centered box and right-justified box both were forced to be jammed into the end of the line. This isn't a problem for something that is right-justified (since it forces it all the way to the right anyway.) Does that make sense?
– killian95
Nov 7 at 21:49
After you placed the first box (which took up the whole width), the centered box and right-justified box both were forced to be jammed into the end of the line. This isn't a problem for something that is right-justified (since it forces it all the way to the right anyway.) Does that make sense?
– killian95
Nov 7 at 21:49
@KieranPaddock did you delete your other question where I directed you to PyPDF?
– killian95
Nov 8 at 0:01
@KieranPaddock did you delete your other question where I directed you to PyPDF?
– killian95
Nov 8 at 0:01
That makes sense, thanks. And I didn't delete it, I just looked at it and for some reason all the comments/answers are gone, though I didn't touch the post.
– Kieran Paddock
Nov 8 at 16:45
That makes sense, thanks. And I didn't delete it, I just looked at it and for some reason all the comments/answers are gone, though I didn't touch the post.
– Kieran Paddock
Nov 8 at 16:45
1
1
@KieranPaddock Ahh, ok. Looks like a mod deleted my answer -- presumably because it was only a link? That's why I couldn't find it in my history. However, I still think it was a useful question and answer pair so I reposted an answer with a simple demo, for what it's worth.
– killian95
Nov 8 at 17:51
@KieranPaddock Ahh, ok. Looks like a mod deleted my answer -- presumably because it was only a link? That's why I couldn't find it in my history. However, I still think it was a useful question and answer pair so I reposted an answer with a simple demo, for what it's worth.
– killian95
Nov 8 at 17:51
add a comment |
up vote
0
down vote
You Could make your cell the same size as the page so that the
self.cell()
can center it to that size/orientation
add a comment |
up vote
0
down vote
You Could make your cell the same size as the page so that the
self.cell()
can center it to that size/orientation
add a comment |
up vote
0
down vote
up vote
0
down vote
You Could make your cell the same size as the page so that the
self.cell()
can center it to that size/orientation
You Could make your cell the same size as the page so that the
self.cell()
can center it to that size/orientation
answered Nov 7 at 21:03
krflol
51728
51728
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197642%2fcenter-the-page-number-in-a-footer-using-pypdf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown