Need to save txt file as word doc using specific template.

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

I'm trying to use less paper and my Unix based business system routinely prints a lot of multiple page reports that I rarely need. I can print these reports to a temporary spool file instead of wide-format listing paper, but they're not in an easily read layout being designed for a wide dot-matrix printer.

I have an automated FTP connection that one-way syncs these '.spl' text files with a folder on my Mac.

My current workflow is to:

1/ Manually load each text file into MS Word using a specific Word template created to fit the full report page width and length.

2/ Manually strip the non-printing page feed characters at the end (needed by our tractor feed printer)

3/ Use a Hazel Rule to save the resulting formatted .doc file to PDF and archive it.

I'd like to automate steps 1 & 2 as well, the first and most pressing problem is obviously "How do I automatically load a text file into Word using a specific word template"?

As far as stripping the unwanted page feed & formatting characters, it'd probably make more sense for me to strip them from the text file BEFORE loading into word. Basically it's everything AFTER a line that reads "--- END OF REPORT ---".

Any suggestions?
Thanks,
Mark.
MacOCD
 
Posts: 44
Joined: Fri Sep 26, 2014 11:02 am

You'll most likely need to write an AppleScript/JavaScript to handle Word. You may want to search an AppleScript specific site for help on this as it's really better suited for this type of thing.
Mr_Noodle
Site Admin
 
Posts: 11240
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Edited 18-04-2016 to show the full solution to how I resolved my problem

It was actually easier than I thought it would be, a single line using the terminal 'textutil' command

An embedded shell script of:-

Code: Select all
textutil -convert doc $1

Then I realised I could set the font and size at the same time:-

Code: Select all
textutil -convert doc -font "Courier New" -fontsize 8 $1


To set page orientation & margins I had to use a second Hazel rule to run an Applescript on the resulting .doc file. I also needed the text to all be Bold to be more readable as I'm actually having to use fontsize 5.5 (instead of size 8 in the sample code above) to fit a page width of my reports on a Landscaped A4 sheet.
Code: Select all
-- Set Page orientation & margins in MS Word.
tell application "Microsoft Word"
   open theFile
   
   -- Change Page Orientation to Lanscape & Set Margins
   set theFile to active document
   set orientation of page setup of section 1 of active document to orient landscape
   set pmSales to page setup of active document
   set left margin of pmSales to (inches to points inches 0.2)
   set right margin of pmSales to (inches to points inches 0.2)
   set top margin of pmSales to (inches to points inches 0.3)
   set bottom margin of pmSales to (inches to points inches 0.2)
   
   -- Change Font type to BOLD here
   set myRange to create range active document start 0 end 0
   set myRange to change end of range myRange by a story item extend type by selecting
   set bold of font object of myRange to true
   
   save active document
   close active document
end tell


I thought this might help someone else.
MacOCD
 
Posts: 44
Joined: Fri Sep 26, 2014 11:02 am

Neat. Hadn't heard of textutil before (or maybe I did but forgot). In any case, glad you were able to put something together that works for you.
Mr_Noodle
Site Admin
 
Posts: 11240
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I actually came across an odd bug in the textutil conversion process where on occasion the resulting .doc can't be opened by MS Word via Applescript. I get a message saying the filetype is blocked.
Image
I can manually open the .doc using Word though.

I've resolved this by getting textutil to convert the text file to .rtf instead of .doc and all works just great.
MacOCD
 
Posts: 44
Joined: Fri Sep 26, 2014 11:02 am

MacOCD wrote:I actually came across an odd bug in the textutil conversion process where on occasion the resulting .doc can't be opened by MS Word via Applescript. I get a message saying the filetype is blocked.


It could be due to textutil converting to doc, which is the very old Word format, and it might not be converting it properly. Try changing the textutil -convert option to docx instead.


MacOCD wrote:I also needed the text to all be Bold to be more readable as I'm actually having to use fontsize 5.5 (instead of size 8 in the sample code above) to fit a page width of my reports on a Landscaped A4 sheet.


Since this is primarily going to be used for PDF viewing on a computer, you are not limited to a physical paper size. Instead of using A4 paper dimensions (8.26"x11.69"), try setting the document dimensions to any size you wish. If you want to stay with defined sizes, try Super A (8.94"x14") or Super B (13"x19"). With a landscaped Super B page, the extra 7" will allow you to increase the font size back to 8 or even perhaps 10! :shock:

If you might have to print the reports but want to use a huge custom PDF size, keep the aspect ratio the same as the paper so it will scale down nicely. For example, the A4 ratio is about 0.7 (8.26/11.69=0.7065868263) so when creating a custom [landscape] page size of 30" width, the height should be 21".



Another option is to skip the Word steps and convert the text directly to PDF. There are a few command line utilities to do it out there but some are very complicated. The simplest utility I've found is text2pdf. He doesn't offer an OSX binary to download so I've downloaded the source and compiled it. You can download it here.

The help text shows the command options
Code: Select all
text2pdf [options] [filename]

  text2pdf makes a 7-bit clean PDF file (version 1.1) from any input file.
  It reads from standard input or a named file, and writes the PDF file
  to standard output.

  There are various options as follows:

  -h            show this message
  -f<font>      use PostScript <font> (must be in standard 14, default: Courier)
  -I            use ISOLatin1Encoding
  -s<size>      use font at given pointsize (default 10)
  -v<dist>      use given line spacing (default 12 points)
  -l<lines>     lines per page (default 60, determined automatically
                if unspecified)
  -c<chars>     maximum characters per line (default 80)
  -t<spaces>    spaces per tab character (default 8)
  -F            ignore formfeed characters (^L)
  -A4           use A4 paper (default Letter)
  -A3           use A3 paper (default Letter)
  -x<width>     independent paper width in points
  -y<height>    independent paper height in points
  -2            format in 2 columns
  -L            landscape mode


For example, to convert report.txt to a PDF with the Super B page landscape dimensions, using Courier New font, size 18, line spacing of 20, and 160 characters per line, the command would be
Code: Select all
text2pdf -x1368 -y936 -fCourierNewPSMT -s18 -v20 -c160 report.txt > report.pdf


Note that the font name must be the PostScript name, not the human friendly name (Courier New). To get the PostScript name, open the Font Book application, locate the font and style you want and choose the View>Show Font Info menu (or press Cmd+I). The first line in the list should be the PostScript name you need. If you want to stick with Courier New in bold, that would be CourierNewPS-BoldMT

Here are a couple of PDF examples I've created if you would like to see what it can do.
spook5-250.pdf
spook6-210.pdf

Based on what you specified earlier, these are A4 page sizes in Landscape orientation. When using size 6 font, it could fit 210 characters on each line. Using size 5, it could fit 250 characters. Here are the commands I used to create them.

Code: Select all
text2pdf -fCourierNewPSMT -L -A4 -c250 -s5 -v5 spook.txt > spook5-250.pdf
text2pdf -fCourierNewPSMT -L -A4 -c210 -s6 -v6 spook.txt > spook6-210.pdf


With a bit of tweaking, I'm sure you can make it work with your reports.

Here is an Applescript you can use within a Hazel action to do the conversion. You will want to have the rule actions move, rename, or flag the text file so that Hazel doesn't keep converting the file over and over.


Code: Select all
-- Download and save the text2pdf binary into your desired location
-- Change the 2 lines below to where you put the text2pdf binary
--   and any options you wish to pass to it for each conversion

set _text2pdf to "/usr/local/bin/text2pdf"
set _args to "-fCourierNewPSMT -L -A4 -c250 -s5 -v5"
-- If you don't need to pass args, don't delete it! Just set it to ""

-- You should not have to change anything below here

tell application "Finder" to set _parent_dir to (container of (item theFile) as text)
tell application "Finder" to set _theFile_POSIX to POSIX path of theFile
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set _newName to the last text item of (theFile as text)
set AppleScript's text item delimiters to oldDelimiters
if _newName contains "." then
   set _newName to (the reverse of every character of _newName) as string
   set x to the offset of "." in _newName
   set _newName to (text (x + 1) thru -1 of _newName)
   set _newName to (the reverse of every character of _newName) as string
end if
tell application "Finder" to set _thePDF_POSIX to POSIX path of (_parent_dir & _newName & ".pdf")
set _shell_cmd to _text2pdf & " " & _args & " " & _theFile_POSIX & " > " & _thePDF_POSIX
try
   tell me to do shell script _shell_cmd
on error errText number errNum
   return {hazelOutputAttributes:{errNum, errText}}
end try
Smudge
 
Posts: 34
Joined: Mon Jul 15, 2013 10:44 pm

Hi Smudge, thanks for your contribution here... I didn't spot the notification so it's been a few days since your post. I am grateful.

Smudge wrote:Since this is primarily going to be used for PDF viewing on a computer, you are not limited to a physical paper size. Instead of using A4 paper dimensions (8.26"x11.69"), try setting the document dimensions to any size you wish. If you want to stay with defined sizes, try Super A (8.94"x14") or Super B (13"x19"). With a landscaped Super B page, the extra 7" will allow you to increase the font size back to 8 or even perhaps 10! :shock:

If you might have to print the reports but want to use a huge custom PDF size, keep the aspect ratio the same as the paper so it will scale down nicely. For example, the A4 ratio is about 0.7 (8.26/11.69=0.7065868263) so when creating a custom [landscape] page size of 30" width, the height should be 21".


The reports formatted for A4 paper in Word are perfectly readable for me, a lot of them are actually in size 8, where they still fit on a page.

Another option is to skip the Word steps and convert the text directly to PDF. There are a few command line utilities to do it out there but some are very complicated. The simplest utility I've found is text2pdf. He doesn't offer an OSX binary to download so I've downloaded the source and compiled it. You can download it here.



That option really appeals to me, thank you. I've downloaded your binary and will give it a try. It would be an awful lot cleaner than using Word.

Thank you so much for your help & detailed instructions. I had actually sorted the problem myself but now I'm going to re-do everything with your, much better method.

Mark.
MacOCD
 
Posts: 44
Joined: Fri Sep 26, 2014 11:02 am

An update to thank Smudge once again for his help in converting .txt files to .pdf

It's taken a lot of trial & error but I've got my solution completely sorted now using txt2pdf.

The applescript supplied didn't like file names (and possibly also folder names) with a space in them and I just couldn't get it working.

With the help of my daughter I used a quick & dirty shell script instead:

Code: Select all
# Quick & dirty txt2pdf Hazel shell script by Iris Ward
# Using Smudges compiled text2pdf.app which was available
# at http://www.insomniacsoftware.com/forums/hazel/text2pdf
# (See  http://www.noodlesoft.com/forums/viewtopic.php?f=4&t=6660 for details)

inputFile="$1"

# Remove the .txt file extension
fileNoExt="${1%.txt}"

# Create output filename based on original name
outputFile="$fileNoExt.pdf"

# For me the font size changes depending on source document so easier to use a variable
fontSize=8

#Example Args for [CourierNew - Landscape - A4 Sized - Max250CharacterLines - FontSize - 8 Point Line Spacing]
args="-fCourierNewPSMT -L -A4 -c250 -s$fontSize -v8"

# $args left intentionally unquoted so the individual globs expand
/usr/local/bin/text2pdf.app $args "$1" > "$outputFile"


This works perfectly of me, it's faster, it creates smaller sized PDF files than my original script using MS Word and doesn't suffer from errors based on Word's environment space.

Slight limitations are I had no control over margins or font weight as I did with word but it's a great solution for me.

Thanks again Smudge!

Mark.
MacOCD
 
Posts: 44
Joined: Fri Sep 26, 2014 11:02 am


Return to Support