-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConvertmd2docx.py
108 lines (94 loc) · 3.72 KB
/
Convertmd2docx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'''
Script reads a text file with a list of files,
combines files into a single file, fixes few things
and converts formats using pandocs.
Input file needs to be in the same directory as the files it is listing
Paths require linux convention with /, windows convention of \\ will bomb.
Requires pandocs installed from https://pandoc.org/installing.html.
Read pandocs document regarding possible values for strOrigionalFormat and strConvert2
This script was written for python 3 and tested under Python Version 3.6.5
Author Siggi Bjarnason Copyright 2019
Website https://www.supergeek.us/
'''
import sys
import os
import time
import subprocess as proc
# Defining stuff
strFilein = "C:/book/source/manuscript/Book.txt"
strOutPath = "C:/book/"
strOutFile = "testOnlineSafety.md"
strConvertedFileName = "testOnlineSafety.docx"
strIndexFile = "OnlineSafetyIndex.txt"
strOrigionalFormat = "markdown"
strConvert2 = "docx"
lstStr2Delete = []
lstStr2Delete.append("{frontmatter}")
lstStr2Delete.append("{book: true, sample: true}")
lstStr2Delete.append("{mainmatter}")
lstStr2Delete.append("{book: true, sample: false}")
lstStr2Delete.append("{backmatter}")
dictReplacements = {}
dictReplacements["]("] = "](C:/book/source/manuscript/resources/"
# Doing stuff
print ("This is a script to combine bunch of files into a single markdown file, then convert it to docx."
"\n This is running under Python Version {0}.{1}.{2}".format(
sys.version_info[0],sys.version_info[1],sys.version_info[2]))
now = time.asctime()
print ("The time now is {}".format(now))
if os.path.isfile(strFilein) :
print ("Found input file, good to go")
else:
print ("could not find input file. Please validate the strFilein variable. exiting")
sys.exit()
if not os.path.exists (strOutPath) :
os.makedirs(strOutPath)
print ("\nPath '{0}' for the output files didn't exists, so I create it!\n".format(strOutPath))
if strOutPath[-1:] != "/":
strOutPath += "/"
strFullIndexFile = strOutPath + strIndexFile
strFullOutFile = strOutPath + strOutFile
print ("Output will be saved to {}".format(strFullOutFile))
strFullconverted = strOutPath + strConvertedFileName
print ("converted file will be written to {}".format(strFullconverted))
iLoc = strFilein.rfind("/")
strInPath = strFilein[:iLoc+1]
print ("Base path is: {}".format(strInPath))
objFileIn = open(strFilein,"r")
objFileOut = open(strFullOutFile,"w")
objIndexOut = open(strFullIndexFile,"w")
strLine = "nada"
while strLine:
strLine = objFileIn.readline()
if strLine.strip() != "":
strInFile = strInPath + strLine.strip()
if os.path.isfile(strInFile) :
objtmpIn = open(strInFile,encoding='utf-8')
strFileText = objtmpIn.read()
objtmpIn.close()
strFileText = strFileText.encode("ascii","ignore")
strFileText = strFileText.decode("ascii","ignore")
for strFind in lstStr2Delete:
strFileText = strFileText.replace(strFind,"")
for strReplace in dictReplacements:
strFileText = strFileText.replace(strReplace,dictReplacements[strReplace])
objFileOut.write (strFileText+"\n")
iStart = strFileText.find("#")
iEnd = strFileText.find("\n",iStart)
# print ("first line is {} char long".format(iLoc))
# print ("First line of {} is: {}".format(strInFile,strFileText[iStart:iEnd]))
objIndexOut.write ("{},{}\n".format(strInFile,strFileText[iStart:iEnd]))
print ("{} is a done!".format(strInPath + strLine.strip()))
else:
print ("{} is NOT a valid file!".format(strInPath + strLine.strip()))
objFileIn.close()
objFileOut.close()
objIndexOut.close()
objFileIn = None
objFileOut = None
objIndexOut = None
objtmpIn = None
strCmdLine = "pandoc {} -f {} -t {} -o {}".format(strFullOutFile,strOrigionalFormat,strConvert2,strFullconverted)
print ("executing {}".format(strCmdLine))
proc.Popen(strCmdLine)
print ("All done !!! :-D")