Wrong encoding in Excel file

I have this script that inserts two UTF-8 files into a new Excel document. The encoding of the German umlauts (ä, ö, Ü etc.) is wrong. How can I fix this?

set filePath to (path to desktop as text) & "de.txt"

try
	set sourceContent to read file filePath
end try

-- Split the content into paragraphs
set sourceParagraphs to paragraphs of sourceContent

set filePath to (path to desktop as text) & "nl.txt"

try
	set targetContent to read file filePath
end try

-- Split the content into paragraphs
set targetParagraphs to paragraphs of targetContent

tell application "Microsoft Excel"
	-- Activate Excel
	activate
	
	-- Create a new workbook
	set newWorkbook to make new workbook
	
	-- Get the first worksheet
	set targetSheet to active sheet
	
	-- Counter for row number
	set rowNumber to 1
	
	-- Write each paragraph to the first column
	repeat with currentParagraph in sourceParagraphs
		-- Skip empty paragraphs
		if length of currentParagraph as string is greater than 0 then
			-- Write to cell in column A
			set targetCell to cell rowNumber of column 1
			set value of targetCell to currentParagraph
			
			-- Increment row counter
			set rowNumber to rowNumber + 1
		end if
	end repeat
	
	set rowNumber to 1
	
	-- Write each paragraph to the first column
	repeat with currentParagraph in targetParagraphs
		-- Skip empty paragraphs
		if length of currentParagraph as string is greater than 0 then
			-- Write to cell in column B
			set targetCell to cell rowNumber of column 2
			set value of targetCell to currentParagraph
			
			-- Increment row counter
			set rowNumber to rowNumber + 1
		end if
	end repeat
	
	set desktopPath to (path to desktop as text) & "import_tm.xlsx"
	set desktopFile to POSIX path of desktopPath
	save active workbook in desktopFile as workbook -- normal format
	
end tell

Hi @mactranslator.

I’m afraid I can’t help with Excel. But the problem’s probably the way the files are being read. Try changing those lines to :

set sourceContent to (read file filePath as «class utf8»)

… and …

set targetContent to (read file filePath as «class utf8»)

For historical reasons, the read command interprets file contents as MacRoman-encoded text unless it’s told to expect something else.

1 Like

Thank you for your help, Nigel!