How to get the exact number of characters in a MS Word document using IronPython -
i'm using word.wdstatistic.wdstatisticcharacterswithspaces
object of microsoft.office.interop.word
total number of characters in word document. however, got different results in term of total characters declared inside word application, when open , checked manually.
here code:
import clr import sys clr.addreference("microsoft.office.interop.word") import microsoft.office.interop.word word def count_characters(docfile): word_application = word.applicationclass() word_application.visible = false document = word_application.documents.open(docfile) ranges = document.content ranges.select() total_characters = ranges.computestatistics(word.wdstatistic.wdstatisticcharacterswithspaces) document.close() word_application.quit() return total_characters if __name__ == '__main__': if len(sys.argv) > 1: docfile = sys.argv[1] number = count_characters(docfile) print(number)
Comments
Post a Comment