I’m a beginner to Powershell and CLI in general, but this task does not need to use either so I’m open to using other tools.

I’m trying to do the following:

  1. Create multiple files from a Word template.
  2. Rename each file based on a list of names found in an Excel/CSV sheet.

Thanks in advance!

  • lemmy___user@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    8 months ago

    You should be able to use COM interoperability , specifically the OpenAsDocument() and Save() methods. You can also use COM interoperability to read from Excel, but using ImprtFrom-CSV would be much easier. Then you just iterate over the file names, and for each one open the template as a document, then save it as the filename.

  • purplemonkeymad@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    8 months ago

    For csv import, use import-csv and loop on the results:

    Import-csv myfile.csv | foreach-object {
    

    Templates should be easy, just copy the template to a new file with the docx extension. Use one of the columns (in this case “name” as the column header,) from the csv for the name:

        $newname = $_.name + '.docx'
        Copy-item 'template.dotx' $newname
    }
    
    • BobTheDestroyer@lemy.lolOP
      link
      fedilink
      arrow-up
      1
      ·
      8 months ago

      Hey this worked for me yesterday but now I’m having trouble getting it to work again. It just outputs a Word doc titled ‘.docx’ now.

    • BobTheDestroyer@lemy.lolOP
      link
      fedilink
      arrow-up
      1
      ·
      8 months ago

      So I’m new to all this. When I enter the first command with my csv file and doc file included, it just opens the word doc. Wasn’t sure what I should do next.