Giter Club home page Giter Club logo

read_word_header_win32com's Introduction

I have a DOCX file, which has the following info

pages of section header
section 1 1 None
section 2 2 Abstract. current page ??, total IV pages
section 3 5 paper body, current page ??, total 35 pages

Then if the line oSec.Headers(1).Range.Fields.Update is used in the following VBA code, the corrected header text will be shown

Function myTrim(s)
    a = Replace(s, vbLf, "")
    myTrim = Trim(a)
End Function

Sub displayHeader()
    idx = 1
    For Each oSec In ActiveDocument.Sections
		oSec.Headers(1).Range.Fields.Update 'this line must be called
        MsgBox "sec " & idx & " " & myTrim(oSec.Headers(1).Range.Text)
        idx = idx + 1
    Next
End Sub

Then I coined the Python version, as we all know it looks like the original VBA one

import win32com
from win32com.client import Dispatch, constants

#~ word = win32com.client.Dispatch('Word.Application')

word = win32com.client.gencache.EnsureDispatch('Word.Application')

word.Visible = 1
word.DisplayAlerts = 0

word.Documents.Open('r:/test.docx')

for idx, oSec in enumerate(word.ActiveDocument.Sections):
    #~ oSec.Headers(1).Range.Fields.Update()
    print(f'sec {idx+1}', oSec.Headers(1).Range.Text.strip())

word.Documents.Close(constants.wdDoNotSaveChanges)
word.Quit()

However the Python code does not give the same corrected header text:

Dispatch('Word.Application') gencache.EnsureDispatch('Word.Application')
use Range.Fields.Update() sec 1
sec 2 Abstract. current page I, total I pages
sec 3 paper body, current page 1, total 1 pages
sec 1
sec 2 Abstract. current page I, total I pages
sec 3 paper body, current page 1, total 1 pages
do not use Range.Fields.Update() sec 1
sec 2 Abstract. current page III, total IV pages
sec 3 paper body, current page 1, total 35 pages
sec 1
sec 2 Abstract. current page I, total I pages
sec 3 paper body, current page 1, total 35 pages

So, what is the problem, and how to fix it? Thank you in advance.

read_word_header_win32com's People

Contributors

retsyo avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.