0

I am trying to convert .doc documents to .docx documents using python. Getting inspiration from this post, I have tried the following code :

import subprocess
import glob
import os

root = "//xxx/"
data_path = root + '/data2/'

os.chdir(data_path)

for doc in glob.iglob("*.doc"):
    
    print(doc)
    subprocess.call(['soffice', '--headless', '--convert-to', 'docx', doc], shell = True) 

But unfortunately litterally nothing happens, i.e. I get no error message, the code is running, the docs are detected (which I check thanks to print) but I don't get any result. Any idea how I may troubleshoot this ?

EDITS :

  • I am running on Windows, hence shell = True
  • I have tried double quotes : '"
  • I have tried without spaces in the names
  • When I execute the subprocess command on one file alone, I get 1as output, which I don't knowhow to interpret...
Vincent
  • 482
  • 6
  • 22
  • Running on Windows or Linux? – Mad Physicist Oct 31 '18 at 18:01
  • On Linux, turn off the shell option. On Windows, put your file name in double quotes: `'"{}"'.format(doc)` – Mad Physicist Oct 31 '18 at 18:01
  • Do the files have spaces in their names? – Mad Physicist Oct 31 '18 at 18:02
  • @MadPhysicist thanks for your answers. I have added the answers in the original post. – Vincent Oct 31 '18 at 18:39
  • Just open libreoffice once and then run the code. Reply if it works. – Vishnudev Krishnadas Oct 31 '18 at 18:40
  • 1
    I believe `1` means there's an error (like if the interpreter exit successfully it returns `code=0`, but if exit due to an error it returns `code=1`). Are you sure the file exist, and you have `soffice`? What happens when you `subprocess.call('soffice')`? Odd you say it doesn't give you an error though. – r.ook Oct 31 '18 at 19:11
  • @Vishnudev @Idlehands thanks for the feedback and sorry for the delay in my reply. This seems to be the issue indeed but I have no idea how to install `soffice` / install and open `libreoffice`. Would you have a link to a good tutorial to learn how to use those ? – Vincent Nov 21 '18 at 16:05
  • So I installed libreoffice and opened it before running the code but I still get nothing. alternatively I tried running `os.system` but still without success. By the way if I run `os.system("dir")` I get error code 255, which corresponds to "The extended attributes are inconsistent."... – Vincent Nov 23 '18 at 17:26

0 Answers0