简体   繁体   中英

Python and VBA, mixed end-of-line characters

Basically I have a python script saved in a Word document, and I modify it using Excel VBA (insert values from Spreadsheet) and print it onto a .py file , to be run in command line. However, when I print the script in the word document onto .py file and open the .py file, the IDE warns "mixed end-of-line characters that will be automatically fixed", and then all the "" in the script become something like ¡§¡¨¡¨¡¨. Below is my vba code:

Dim wddoc As Variant
Dim wdApp As Variant
Dim fileopen As String

Set wdApp = CreateObject("Word.application")
Set wddoc = wdApp.Documents.Open("C:\Users\BLABLA\Desktop\python test\test.docx")

'modifying word document here, inserting value from spread sheet

fileopen = "C:\Users\BLABLABLA\Desktop\python test\testpy.py"

Open fileopen For Append As #1
Print #1, wddoc.Content
Close #1

With wdApp.activedocument
.Close WdDoNotSaveChanges
End With

and below is an example of the script saved in word document

# -*- coding: utf-8 -*-


for i in range(1,10):
    print(“test”)
       “ ”appears to be wrong

and below is what I got when I print it onto a .py file:

# -*- coding: utf-8 -*-

for i in range(1,10):
    print(¡§test¡¨)
    ¡§¡¨appears to be wrong

How can I fix that? I am pretty newb and it maybe a silly question but I'll appreciate any input. Thanks in advance!

The definitive answer is:

Never store source code in Microsoft Word

Your end-of-line characters are a mixture of carriage returns, line-feeds, and paragraph-marker characters. Your quote marks are a mixture of single quotes, double-quotes, and mirrored inverted-comma and double inverted-comma quoted-speech markers.

Microsoft Word is helpfully 'autocorrecting' them for you, into the best format for reading and publishing a beautiful business letter in American English.

Gee, thanks, Microsoft... But then again, that's what Word is for: formatted human-readable documents.

Source code is saved in plain text files - .txt or the file type for your language and development environment - and you never, ever, open a source file in Microsoft Word.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM