-1

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?

  • Did you find out how to read a doc/x file in python? Parse an empty file and inspect the parsed object to see what it contains (or doesn't contain). Then do the same for a non-empty file. Now you know how to check if it's empty. – Pranav Hosangadi Mar 02 '22 at 22:08

1 Answers1

0

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