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
    43 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.

  • @mikyopii@programming.dev
    link
    fedilink
    33 months ago

    Does it have to be a Word template? If you can get that into a plain text format this will be much easier. There may be a module that you can download online otherwise you are probably going to have to do some painful parsing after you import the file using “Get-Content”.

    Do you have any code written? This sounds like something that ChatGPT could probably handle pretty well.

  • 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
    }
    
  • @Vince@lemmy.world
    link
    fedilink
    2
    edit-2
    3 months ago

    Sounds like you’re just copying a template to new files based on a csv? You don’t have to edit anything inside the word files at all?

    If that’s the case, just load the csv file, I think the command is Read-Content, loop through each line, split by ‘,’ parse whatever name data you need and copy your template.docx to your new file.docx

  • stewie410
    link
    fedilink
    1
    edit-2
    3 months ago

    If the Excel/CSV sheet is actually a CSV file, Import-Csv in powershell will return the content as an array of objects, where each row is one element in the array.