Use pywin32 to go to specified page in word doc

JohnnyW

I have a long word document with over 100 tables. I am trying to allow users to select a page number via python to enter data into the table on the specified page within the word document. I am able to enter data into a table with the following code, but the problem is that the document is so long, it's not easy for a user to know which table number they are on when they are 80 pages into the word document (not every page has a table and some pages have multiple tables).

import win32com.client as win32    
word = win32.Dispatch("Word.Application")
word.Documents.Open(my_document_path)
doc = word.ActiveDocument
table = doc.Tables(51) #random selection for testing purposes
table.Cell(Row = 7, Column = 2).Range.Text = "test"

So what I need help with is extracting the table number on a page in a word document that is specified via user input (i.e., user specifies that they want to add data to page 13 so the code will determine that table 51 is on page 72).

If I record a macro in word for simply jumping to a page, this is the VB code...

Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="13"

I have tried translating this into Python using the following line of code, but it's not jumping to the correct page.

doc.GoTo(win32.constants.wdGoToPage, win32.constants.wdGoToNext, "13")
Cindy Meister

GoTo works with the Selection object, which is a property of the Word application, not a document. In the code in the question, word represents the Word application, so word.Selection.GoTo should work.

Note the subsitution of wdGoToAbsolute in the GoTo method call for wdGoToNext - that's "safer" for going to a specific page number.

In order to get the entire Range for a page it's possible to use a built-in bookmark name "\Page". This only works for the page where the selection is, which is why it's necessary to first go to the page. It's then possible to get the first table (or any other table index) on the page.

If the index number of the table in the document is also required, that can be calculated by getting the document's range, then setting the end-point to the end of the page's range.

import win32com.client as win32    
word = win32.Dispatch("Word.Application")
word.Documents.Open(my_document_path)
doc = word.ActiveDocument
word.Selection.GoTo(win32.constants.wdGoToPage, win32.constants.wdGoToAbsolute, "13")
rngPage = doc.Bookmarks("\Page").Range
table = rngPage.Tables(1) #first table on the page
table.Cell(Row = 7, Column = 2).Range.Text = "test"

#rngToPage = doc.Content
#rngToPage.End = rngPage.End
#tableIndex = rngToPage.Tables.Count

Note that I don't work with Python, so I'm not able to test the Python code. So watch out for syntax errors. For this reason, I've appended the VBA code I used to test the approach.

Sub GetTableCountOnPage()
    Dim tbl As Word.Table
    Dim sPage As String
    Dim rngPage As Word.Range

    sPage = InputBox("On which page is the table?")
    Selection.GoTo What:=wdGoToPage, Name:=sPage
    Set rngPage = Selection.Document.Bookmarks("\Page").Range
    If rngPage.Tables.Count > 0 Then
        Set tbl = rngPage.Tables(1)
        tbl.Select

        Dim rngToTable As Word.Range
        Set rngToTable = Selection.Document.content
        rngToTable.End = rngPage.End
        Debug.Print rngToTable.Tables.Count & " to this point."
    End If
End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pywin32 Adding new page to a word document

From Dev

Pywin32 how to replace text in entire word document

From Dev

How to use PyWin32 to return Excel row?

From Dev

flextable autofit in a Rmarkdown to word doc causes table to go outside page margins

From Dev

Read a MS Word (doc) file page by page

From Dev

How do I use VBA to insert an image to the second page of a word doc

From Dev

docx-python word doc page break

From Dev

Not able to use the win32gui module from pywin32 in Pycharm

From

How to use go doc in public repo

From Dev

Trying to use win32ui with pywin32 gives: A dynamic link library (DLL) initialization routine failed

From Dev

Opening a word doc from excel, print a selection and a string on the same page

From Dev

GetGUIThreadInfo() with pywin32

From Java

How can I use pywin32 with a virtualenv without having to include the host environment's site-packages folder?

From Dev

Sorting with xlwings (pywin32)

From Dev

PyWin32 and Python 3.8.0

From Dev

How to allow go to specified page in free jqgrid pager

From Dev

Is it possible to go back to specified page id in jQuery mobile?

From Dev

Formatting Like a Word Doc

From Dev

Variables in MS Word doc

From Dev

css print mode: display header and footer only on first page of a generated word doc

From Dev

Shapes.AddTextbox being placed at wrong position in Word doc after the first page

From Dev

how to use URL and go to PDF bookmark page

From Dev

Use JavaScript or MVC razor to go back a page

From Dev

How to make Pagination "next page" and "previous page" go to the specified href inside a form?

From Dev

ImportError: No module named pywin32

From Dev

Getting this error using pywin32 module?

From Dev

Pywin32 Windows service reading directory

From Dev

How to update pywin32 automatically?

From Dev

Pywin32 --> COM -->Powerpoint tutorial?

Related Related

  1. 1

    Pywin32 Adding new page to a word document

  2. 2

    Pywin32 how to replace text in entire word document

  3. 3

    How to use PyWin32 to return Excel row?

  4. 4

    flextable autofit in a Rmarkdown to word doc causes table to go outside page margins

  5. 5

    Read a MS Word (doc) file page by page

  6. 6

    How do I use VBA to insert an image to the second page of a word doc

  7. 7

    docx-python word doc page break

  8. 8

    Not able to use the win32gui module from pywin32 in Pycharm

  9. 9

    How to use go doc in public repo

  10. 10

    Trying to use win32ui with pywin32 gives: A dynamic link library (DLL) initialization routine failed

  11. 11

    Opening a word doc from excel, print a selection and a string on the same page

  12. 12

    GetGUIThreadInfo() with pywin32

  13. 13

    How can I use pywin32 with a virtualenv without having to include the host environment's site-packages folder?

  14. 14

    Sorting with xlwings (pywin32)

  15. 15

    PyWin32 and Python 3.8.0

  16. 16

    How to allow go to specified page in free jqgrid pager

  17. 17

    Is it possible to go back to specified page id in jQuery mobile?

  18. 18

    Formatting Like a Word Doc

  19. 19

    Variables in MS Word doc

  20. 20

    css print mode: display header and footer only on first page of a generated word doc

  21. 21

    Shapes.AddTextbox being placed at wrong position in Word doc after the first page

  22. 22

    how to use URL and go to PDF bookmark page

  23. 23

    Use JavaScript or MVC razor to go back a page

  24. 24

    How to make Pagination "next page" and "previous page" go to the specified href inside a form?

  25. 25

    ImportError: No module named pywin32

  26. 26

    Getting this error using pywin32 module?

  27. 27

    Pywin32 Windows service reading directory

  28. 28

    How to update pywin32 automatically?

  29. 29

    Pywin32 --> COM -->Powerpoint tutorial?

HotTag

Archive