I want to convert every .xlsx file that is added to a folder to pdf

I know can watch folders I’m just not sure how to do it without opening excel

Apologies for the stupid question, but what's a clam?

-rob.

omg what an embarrassing autocorrect :man_facepalming:

xlsx*

Welcome to the forum.

I've de-auto-corrected the title of this thread. I hope someone will be able to help you find a solution.

1 Like

Looking into this, I see that if you install LibreOffice, it is possible to convert .xlsx files to .pdf in the command line. See:

Such commands can be run from Keyboard Maestro's Execute a Shell Script action.

Note that KM will not automatically know where to find the libreoffice binary file. See Path in Shell Scripts, but the easiest way, in my opinion, would be just to include the full path to libreoffice in the Execute a Shell Script action. You will be able to get that in the Terminal with the command which libreoffice.

I did some web searching, and … well, @kevinb was quicker than I was :). Here's one additional link on that subject, though:

After not finding any better solutions via search, I asked ChatGPT. Its first answer is the same as above, to use LibreOffice. The second answer involved a convoluted path through CSV and HTML to PDF, and requires installing a couple Unix utilities via Homebrew. I don't believe this would get very good results, but if you ask your LLM of choice a question about converting .xlsx to PDF on macOS in Terminal, you may see the same answers I did.

Other than the above, I think the answer is to have them open (briefly) in Excel, then have KM do a File > Print > Save as PDF step. Not ideal, I know.

-rob.

1 Like

What's the problem with opening Excel? That you don't have it, or just the annoyance of workbooks appearing unexpectedly when you're doing something else?

If you are trying to avoid Excel altogether then, as others have said, any output will be patchy at best. If you want the PDF to bear any resemblance to the workbook you'll have to open it in Excel (maybe Pages or Libra Office) and generate the PDF from there.

With Excel, you shouldn't need to do a print-to-PDF -- if you provide an output path, this AppleScript should work:

tell application "Microsoft Excel"
	tell active workbook
		save workbook as filename pathToFile file format PDF file format
	end tell
end tell

...and there might be a way to force Excel to remain in the background, to minimise disruption.

But an obvious question is -- why do you want these files converting as soon as they "arrive"? Perhaps a batch conversion while you're at lunch or grabbing a drink would work just as well while reducing disruption to a minimum?

2 Likes

I think you are referring there to @griffman's "The second answer involved a convoluted path through CSV and HTML to PDF".

If it works in LibreOffice in the GUI, I don't see why it should be any different when using LibreOffice in "headless" mode from a shell script.

I don't think the theory suggests that the results should be "patchy at best" for that approach also. As for what happens in practice, I don't know! I'll leave it for the OP to try if interested.

I've never tested LibreOffice's ability to convert Excel files to PDFs, so I thought I'd give it a shot :). The command syntax is pretty simple:

soffice --convert-to pdf filename.xlsx

There are tons of other things you can add to that, but that worked. I gave it a very simple pure-text spreadsheet first, and that was basically fine—the fonts were ever so slightly different, but it was close.

Then I gave it a worksheet with cells formatted as a table. And while it printed it, it basically ignored all the formatting. Here's the original:

And the print from LibreOffice:

This makes sense to me, as the formatting Excel is using there isn't really there. So then I converted the table to actual Excel formatting, and it did a much better job:

It's speedy and definitely UI-free, so depending on what the formatting is of the source files, it could probably do a more than acceptable job. One limitation, though, is that it simply handles the entire file—if you have multiple sheets in a workbook, they'll each print, with a page break between them. And if one of those sheets is filled with data for charts, well, you'll get all that too. (Don't ask me how I know that.)

Still, this is a potentially useful tool and one I'll keep around!

-rob.

1 Like

I'll confess that I was also including going through Libra Office, headless or GUI, because I've seen a lot of formatting issues in the past.

Thankfully I'm woefully out of date[1] on this, as @griffman has shown, so it certainly looks like the best "unobtrusive" method -- I did have a go with AppleScripting Excel, using run rather than activate, but as soon as you open the file Excel springs to the front...

[1] We used to have a lot of Libra Office users, because "Microsoft are evil", and a lot of issues when they shared documents with MS Office users. That was before our central IS went all-in on Exchange/O365/etc and started providing "free" licences -- when people no longer had to pay for Office themselves, MS were suddenly a lot less evil and Libra usage fell to almost nothing. Go figure...

I see. By the way, the name really is "LibreOffice".

It seems that libre wasn't as important as gratis.