this post was submitted on 22 Mar 2024
10 points (100.0% liked)

Programming

17667 readers
260 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
 

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!

top 17 comments
sorted by: hot top controversial new old
[–] [email protected] 4 points 9 months ago (1 children)

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.

[–] [email protected] 1 points 9 months ago (1 children)

Sorry I'm new to this. What exactly do I enter into Powershell for each step?

[–] [email protected] 2 points 9 months ago

Nevermind I got it!

[–] purplemonkeymad 2 points 9 months ago (2 children)

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
}
[–] [email protected] 1 points 9 months ago (1 children)

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.

[–] [email protected] 1 points 9 months ago (1 children)

Actually nevermind that question. Just realized the command was split.

I'm assuming I need to replace the name portion of the 2nd line. What do I input if the data is a list that starts in A1?

[–] [email protected] 1 points 9 months ago

I got it now!

[–] [email protected] 1 points 9 months ago (1 children)

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.

[–] purplemonkeymad 2 points 9 months ago (2 children)

Make sure the columns in your csv are named properly, my code assumes it's named just "name"

[–] [email protected] 1 points 8 months ago

Ah, I think that was it. Forgot about the column heading. Thanks for your help!

[–] [email protected] 1 points 8 months ago

By the way, what would be the equivalent code when using a text file for the list of names rather than a csv file?

[–] mikyopii 2 points 9 months ago (1 children)

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.

[–] [email protected] 2 points 9 months ago (1 children)

It does have to be a Word template. It has a bar graph and various texts.

[–] mikyopii 2 points 9 months ago

Yeah, I looked at the plain text of a .dotx file. That's unparsable. The other poster probably has the right idea on how to get information out of the file.

[–] [email protected] 2 points 9 months ago* (last edited 9 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

[–] [email protected] 1 points 9 months ago* (last edited 9 months ago)

A good / better option for this is AutoIt. It can interact directly with MS Office via internal APIs and it also has a function to read CSVs.

[–] stewie410 1 points 9 months ago* (last edited 9 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.