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:



Screenshot



Thanks for the help.










share|improve this question


























    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:



    Screenshot



    Thanks for the help.










    share|improve this question
























      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:



      Screenshot



      Thanks for the help.










      share|improve this question













      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:



      Screenshot



      Thanks for the help.







      python pdf pypdf






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 20:53









      Kieran Paddock

      691111




      691111
























          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.






          share|improve this answer





















          • 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


















          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






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            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

























            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.






            share|improve this answer





















            • 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















            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.






            share|improve this answer





















            • 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













            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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


















            • 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












            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






            share|improve this answer

























              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






              share|improve this answer























                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






                share|improve this answer












                You Could make your cell the same size as the page so that the



                self.cell()


                can center it to that size/orientation







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 7 at 21:03









                krflol

                51728




                51728






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    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





















































                    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







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()