arda0910
arda0910

Reputation: 47

Converting MS Word files (saved in Drive) to Google Docs using app script

I'm stuck with something and can't find a solution to it. Is there a way to convert MS Word files stored in Google Drive to Google Docs using the file urls or ids? I currently have a spreadsheet with the urls to the files. Alternatively, a python script could be used too. Any help or idea would be appreciated! Thanks!

Upvotes: 2

Views: 1485

Answers (2)

Hao Hoang
Hao Hoang

Reputation: 51

I found a piece of code here.

Image

I tried the code and it worked fine

function convert_word_to_doc() {
  // Replace file_id with your file id
  var docx = DriveApp.getFileById('file_id')
  var newDoc = Drive.newFile();
  var blob = docx.getBlob();
  var file = Drive.Files.insert(newDoc,blob,{convert:true});
  DocumentApp.openById(file.id).setName(docx.getName());
  return file.id
}

Upvotes: 4

Yuri Khristich
Yuri Khristich

Reputation: 14537

It can be done pretty easy. Here is the script for Spreadsheet App (not Python, though):

function main() {

  var ids = [
    '1MSZwvWwZFwiS0LfDYpUL9vvKXCvbeZkO',
    '1BxECZrrIQpKFJYN5zHUeUDxeEiB8NjEb'
  ];

  for (var id of ids)  convert_docx_to_google_doc(id);

}

function convert_docx_to_google_doc(id) {
  var docx       = DriveApp.getFileById(id);
  var folder     = docx.getParents().next();
  var blob       = docx.getBlob();
  var new_file   = Drive.newFile(); // Enable the Advanced Drive API Service
  var google_doc = Drive.Files.insert(new_file, blob, {convert:true});
  
  DriveApp.getFileById(google_doc.id)
    .setName(docx.getName()
    .replace(/\.docx$/,''))
    .moveTo(folder);
}

It takes the array of IDs and saves the docx files as google docs into the same original folders. And removes the '.docx' extensions from the names of google docs files.

But are you sure you need it? Google Docs can open and save MS Word documents natively.

Upvotes: 4

Related Questions