Python源码示例:pypandoc.convert_file()
示例1
def main():
marks_down_links = {
"Standford CS231n 2017 Summary":
"https://raw.githubusercontent.com/mbadry1/CS231n-2017-Summary/master/README.md",
}
# Extracting pandoc version
print("pandoc_version:", pypandoc.get_pandoc_version())
print("pandoc_path:", pypandoc.get_pandoc_path())
print("\n")
# Starting downloading and converting
for key, value in marks_down_links.items():
print("Converting", key)
pypandoc.convert_file(
value,
'pdf',
extra_args=['--latex-engine=xelatex', '-V', 'geometry:margin=1.5cm'],
outputfile=(key + ".pdf")
)
print("Converting", key, "completed")
示例2
def convert_log(file, file_format='html'):
"""
Converts the log file to a given file format
:param file: The filename and path
:param file_format: The desired format
"""
output_filename = os.path.splitext(file)[0] + '.' + file_format
output = pypandoc.convert_file(file, file_format, outputfile=output_filename)
assert output == ""
# The End
示例3
def read_md(f): return convert_file(f, 'rst').replace("~",'^') # Hack to pass the 'rst_lint.py' - PyPI
示例4
def main():
home_link = "https://raw.githubusercontent.com/mbadry1/DeepLearning.ai-Summary/master/"
marks_down_links = {
"Deeplearning.ai summary Homepage":
home_link + "Readme.md",
"01- Neural Networks and Deep Learning":
home_link + "1-%20Neural%20Networks%20and%20Deep%20Learning/Readme.md",
"02- Improving Deep Neural Networks Hyperparameter tuning, Regularization and Optimization":
home_link + "2-%20Improving%20Deep%20Neural%20Networks/Readme.md",
"03- Structuring Machine Learning Projects":
home_link + "3-%20Structuring%20Machine%20Learning%20Projects/Readme.md",
"04- Convolutional Neural Networks":
home_link + "4-%20Convolutional%20Neural%20Networks/Readme.md",
"05- Sequence Models":
home_link + "5-%20Sequence%20Models/Readme.md",
}
# Extracting pandoc version
print("pandoc_version:", pypandoc.get_pandoc_version())
print("pandoc_path:", pypandoc.get_pandoc_path())
print("\n")
# Starting downloading and converting
for key, value in marks_down_links.items():
print("Converting", key)
pypandoc.convert_file(
value,
'pdf',
extra_args=['--pdf-engine=xelatex', '-V', 'geometry:margin=1.5cm'],
outputfile=(key + ".pdf")
)
print("Converting", key, "completed")
示例5
def _load_readme(self):
readme = self._config['package']['readme']
readme_path = os.path.join(self._dir, readme)
if self.has_markdown_readme():
if not pypandoc:
warnings.warn(
'Markdown README files require the pandoc utility '
'and the pypandoc package.'
)
else:
self._readme = pypandoc.convert_file(readme_path, 'rst')
else:
with open(readme_path) as f:
self._readme = f.read()
示例6
def read_md(filename):
try:
from pypandoc import convert_file
return convert_file(filename, 'rst')
except (ImportError, OSError):
return open(filename).read()
示例7
def convert_markdown_to_rst(file):
return convert_file(file, 'rst')
示例8
def long_description(filename = 'README.md'):
if os.path.isfile(os.path.expandvars(filename)):
try:
import pypandoc
long_description = pypandoc.convert_file(filename, 'rst')
except ImportError:
long_description = open(filename).read()
else:
long_description = ''
return long_description
示例9
def from_file(self, filename, **opts):
self.tmp_dir = wd = tempfile.mkdtemp()
target_file = os.path.join(wd, 'post.md')
import pypandoc
pypandoc.convert_file(
filename,
format='docx',
to='markdown-grid_tables',
outputfile=target_file,
extra_args=[
'--standalone',
'--wrap=none',
'--extract-media={}'.format(wd)
]
)
with open(target_file) as f:
md = f.read()
# Image embeddings exported from docx files have fixed sizes in inches
# which browsers do not understand. We remove these annotations.
md = re.sub(r'(\!\[[^\]]+?\]\([^\)]+?\))\{[^\}]+?\}', lambda m: m.group(1), md)
# Write markdown content to knowledge post (images will be extracted later)
self.kp_write(md)
示例10
def read_md(f):
return convert_file(f, 'rst')
# read_md = lambda f: convert(f, 'rst')
示例11
def read_md(path):
long_desc = ""
if os.path.exists(path):
try:
from pypandoc import convert_file
long_desc = convert_file(path, 'rst')
except:
try:
long_desc = open(path, 'r').read()
except:
pass
return long_desc
示例12
def write_index_rst(readme_file=None, write_file=None):
t = Time.now()
t.out_subfmt = "date"
out = (
".. pyuvdata documentation master file, created by\n"
" make_index.py on {date}\n\n"
).format(date=t.iso)
if readme_file is None:
main_path = os.path.dirname(
os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
)
readme_file = os.path.join(main_path, "README.md")
readme_text = pypandoc.convert_file(readme_file, "rst")
# convert relative links in readme to explicit links
readme_text = readme_text.replace(
"<docs/",
"<https://github.com/RadioAstronomySoftwareGroup/pyuvdata/tree/master/docs/",
)
readme_text = readme_text.replace(
"<.github/",
"<https://github.com/RadioAstronomySoftwareGroup/pyuvdata/tree/master/.github/",
)
out += readme_text
out += (
"\n\nFurther Documentation\n====================================\n"
".. toctree::\n"
" :maxdepth: 1\n\n"
" tutorial\n"
" uvdata_parameters\n"
" uvdata\n"
" uvcal_parameters\n"
" uvcal\n"
" uvbeam_parameters\n"
" uvbeam\n"
" uvflag_parameters\n"
" uvflag\n"
" cst_settings_yaml\n"
" utility_functions\n"
" known_telescopes\n"
" developer_docs\n"
)
out.replace(u"\u2018", "'").replace(u"\u2019", "'").replace(u"\xa0", " ")
if write_file is None:
write_path = os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
write_file = os.path.join(write_path, "index.rst")
F = open(write_file, "w")
F.write(out)
print("wrote " + write_file)
示例13
def html_to_docx(htmlfile, docxfile, handler=None, metadata=None):
""" Convert html file to docx file.
Parameters
----------
htmlfile : str
Filename of the notebook exported as html
docxfile : str
Filename for the notebook exported as docx
handler : tornado.web.RequestHandler, optional
Handler that serviced the bundle request
metadata : dict, optional
Dicts with metadata information of the notebook
"""
# check if html file exists
if not os.path.isfile(htmlfile):
raise FileNotFoundError(f'html-file does not exist: {htmlfile}')
# check if export path exists
if os.path.dirname(docxfile) != '' and not os.path.isdir(os.path.dirname(docxfile)):
raise FileNotFoundError(f'Path to docx-file does not exist: {os.path.dirname(docxfile)}')
# set extra args for pandoc
extra_args = []
if metadata is not None and 'authors' in metadata:
if isinstance(metadata['authors'], list) and all(
['name' in x for x in metadata['authors']]
):
extra_args.append(
f'--metadata=author:' f'{", ".join([x["name"] for x in metadata["authors"]])}'
)
elif handler is not None:
handler.log.warning(
'Author metadata has wrong format, see https://github.com/m-rossi/jupyter_docx_bun'
'dler/blob/master/README.md'
)
if metadata is not None and 'subtitle' in metadata:
extra_args.append(f'--metadata=subtitle:{metadata["subtitle"]}')
if metadata is not None and 'date' in metadata:
extra_args.append(f'--metadata=date:{metadata["date"]}')
# convert to docx
pypandoc.convert_file(
htmlfile,
'docx',
format='html+tex_math_dollars',
outputfile=docxfile,
extra_args=extra_args,
)