'How to check if a docx or doc file is empty

I have a script that can convert a docx file to a json and I was wondering how can I detect if a file is empty. A solution I found is that one: https://thispointer.com/python-three-ways-to-check-if-a-file-is-empty/

Using:

  • os.stat(file_path).st_size == 0:
  • os.path.exists(file_path)
  • os.path.getsize(path)

Unfortunately since an empty docx is not equal to 0. I can't use those methods. Any other solution?



Solution 1:[1]

what if you use the docx module?

you can check it here, according to that documentation, you can read the paragraphs and after check the length:

import docx

doc = docx.Document("E:/my_word_file.docx")
all_paras = doc.paragraphs
len(all_paras)

If the lenght is equal to 0 you can assume this is empty. However this only works for .docx files for what I can see

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alejandro Vázquez