Code Monkey home page Code Monkey logo

md2report's Introduction

md2report

一个用于将Markdown文件转换为可以直接提交给学校的实验报告/大作业报告/期末小论文的工具。

如果你的院系/课程要求必须提交docx格式的报告,并且:

  • 你认为word/docx实在是太蠢了,并且习惯于使用markdown编辑文档,md2report能够大幅缩短你在报告格式、docx样式以及排版上花费的时间;
  • 你没有试过使用markdown,不妨尝试一下: Markdown Guides, Mardown 教程

如果你的院系/课程允许提交pdf格式的报告,寻找一个好用的tex模板或许是一个更好的方案。但是考虑到学习成本以及使用难度,md2report仍然可以作为一个替代选项。

预览更多

Preview

Quick Start

虽然md2report使用的都是标准markdown语法,但是markdown标记到docx的样式映射可能与你的习惯不同。 因此,请确保除了本小节内容以外,开始使用md2report之前先阅读文档中关于语法的部分。

Web UI

md2report提供了Web UI, 无需安装即可使用。

WebUI仅为不会使用python的用户提供便利,不能保证可用性,也不保证是最新版本。大体积文件建议使用CLI。

请不要上传zip bomb,如果服务受到攻击可能考虑关闭服务。

CLI

md2report提供了CLI,如果想使用CLI,需要:

  • python 3.10+
  • poetry in PATH
  • pandoc(version >= 2.11) in PATH
  • cxx2flow
    • 可选,仅在使用cxx2flow功能时需要
git clone https://github.com/woolen-sheep/md2report.git 
cd backend
poetry install
poetry shell

python md2report.py -h
# usage: md2report.py [-h] [-c CONFIG] [--highlight HIGHLIGHT] [-o OUTPUT] -i INPUT [-t TEMPLATE]
#
# options:
#   -h, --help            show this help message and exit
#   -c CONFIG, --config CONFIG
#                         config file path
#   --highlight HIGHLIGHT
#                         enable highlight of code blocks
#   -o OUTPUT, --output OUTPUT
#                         output docx filename
#   -i INPUT, --input INPUT
#                         input markdown filename
#   -t TEMPLATE, --template TEMPLATE
#                         template to use
#   --indent-font-size INDENT_FONT_SIZE
#                         first line indent font size in pt
#   --indent-font-num INDENT_FONT_NUM
#                         first line indent num
#   --first_line_indent FIRST_LINE_INDENT
                        enable the first line indent

python md2report.py -i test/test_case/5.2数据结构实验报告.md

# see output.docx

Docker

docker run --name md2report -d woolensheep/md2report:latest

此docker为 backend/Dockerfile build 产物,无法作为完整的前后端使用。但是你可以进入其中运行CLI而无需搭建环境:

➜  no-more-docx-report git:(master) ✗ docker exec -it md2report bash
root@f35bd61ef8bc:/app# python md2report.py -h
usage: md2report.py [-h] [-c CONFIG] [--highlight HIGHLIGHT] [-o OUTPUT] -i INPUT [-t TEMPLATE]

options:
  -h, --help            show this help message and exit
  -c CONFIG, --config CONFIG
                        config file path
  --highlight HIGHLIGHT
                        enable highlight of code blocks
  -o OUTPUT, --output OUTPUT
                        output docx filename
  -i INPUT, --input INPUT
                        input markdown filename
  -t TEMPLATE, --template TEMPLATE
                        template to use
  --first_line_indent FIRST_LINE_INDENT
                        enable the first line indent

Self-Hosted Web UI

md2docx的Web UI也是开源的,你可以使用docker-compose部署。 需要注意的是现在的docker-compose.yaml中挂载了绝对路径,使用之前请先修改。

cd backend
docker compose up --build -d
docker compose ps

Features

目前支持的特性如下:

  • Title and SubTitle
  • Abstract
  • Heading (H1 to H4)
  • Image Caption
  • Table
  • Table Caption
  • Code Highlight
  • Table of Content
  • Header and Footer
  • First line indent
  • Page Numbering
    • Skip numbering of TOC and Abstract
  • Template of Specific School
    • HUST
      • School Logo
      • Student Infomation

由于依赖了pandoc,除了以上内容,pandoc原生支持的markdown语法也应该正常工作。

Compatibility

目前仅在MS Office 2019上进行过测试,测试版本为Microsoft® Word 2019MSO (版本 2210 Build 16.0.15726.20070) 64 位,能够正常打开生成的文档。 打开文档时若提示是否更新文档中的这些域?,请选择。另存文件可以消除该提示。

  • 使用LibreOffice的用户打开生成的文档后,需要手动更新目录。
  • 若使用其它Word编辑器的用户打开后发现目录为空,也可尝试手动更新目录。

Contribute

欢迎PR,欢迎feature request。

在open issue之前请先阅读提问的智慧

在报告bug时应提供:

  • bug现象
  • Office版本
  • 可供复现的输入文件

我不确定是否只有我所在的学校存在报告过多的现象,或者这是一个普遍的现象。如果你有同样的困扰,可以开PR来补充你们学校的template。贡献方式详见 帮助开发

md2report's People

Contributors

earthcompass avatar poorpool avatar woolen-sheep avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

md2report's Issues

运行出现的问题

第一次提示缺少输出 output.docx 文件

D:\university\master\courses\md2report\backend>python md2report.py -i test/test_case/5.2数据结构实验报告.md
Please give me star if this application helps you!
如果这个应用有帮助到你,请给我点一个 star!
https://github.com/woolen-sheep/md2report
Traceback (most recent call last):
  File "D:\university\master\courses\md2report\backend\filters\general.py", line 211, in <module>
    pf.toJSONFilter(process_report)
  File "D:\software\Anaconda3\lib\site-packages\panflute\io.py", line 148, in toJSONFilter
    return run_filter(*args, **kwargs)
  File "D:\software\Anaconda3\lib\site-packages\panflute\io.py", line 227, in run_filter
    return run_filters([action], *args, **kwargs)
  File "D:\software\Anaconda3\lib\site-packages\panflute\io.py", line 200, in run_filters
    doc = load(input_stream=input_stream)
  File "D:\software\Anaconda3\lib\site-packages\panflute\io.py", line 58, in load
    doc = json.load(input_stream, object_hook=from_json)
  File "D:\software\Anaconda3\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "D:\software\Anaconda3\lib\json\__init__.py", line 370, in loads
    return cls(**kw).decode(s)
  File "D:\software\Anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\software\Anaconda3\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "D:\software\Anaconda3\lib\site-packages\panflute\elements.py", line 1384, in from_json
    raise NotImplementedError(f'Unknown tag: {tag}')
NotImplementedError: Unknown tag: Caption
Error running filter D:\university\master\courses\md2report\backend\filters\general.py:
Filter returned error status 1
Traceback (most recent call last):
  File "md2report.py", line 141, in <module>
    convert_md_to_docx(conf)
  File "md2report.py", line 91, in convert_md_to_docx
    handler_map[handler_name](str(output_path.absolute()), **params)
  File "D:\university\master\courses\md2report\backend\docx_handler\hust.py", line 102, in process_hust_docx
    doc: TDocument = Document(filename)
  File "D:\software\Anaconda3\lib\site-packages\docx\api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part
  File "D:\software\Anaconda3\lib\site-packages\docx\opc\package.py", line 128, in open
    pkg_reader = PackageReader.from_file(pkg_file)
  File "D:\software\Anaconda3\lib\site-packages\docx\opc\pkgreader.py", line 32, in from_file
    phys_reader = PhysPkgReader(pkg_file)
  File "D:\software\Anaconda3\lib\site-packages\docx\opc\phys_pkg.py", line 30, in __new__
    raise PackageNotFoundError(
docx.opc.exceptions.PackageNotFoundError: Package not found at 'D:\university\master\courses\md2report\backend\output.docx'

补上 output.docx 后提示如下

D:\university\master\courses\md2report\backend>python md2report.py -i test/test_case/5.2数据结构实验报告.md
Please give me star if this application helps you!
如果这个应用有帮助到你,请给我点一个 star!
https://github.com/woolen-sheep/md2report
Traceback (most recent call last):
  File "D:\university\master\courses\md2report\backend\filters\general.py", line 211, in <module>
    pf.toJSONFilter(process_report)
  File "D:\software\Anaconda3\lib\site-packages\panflute\io.py", line 148, in toJSONFilter
    return run_filter(*args, **kwargs)
  File "D:\software\Anaconda3\lib\site-packages\panflute\io.py", line 227, in run_filter
    return run_filters([action], *args, **kwargs)
  File "D:\software\Anaconda3\lib\site-packages\panflute\io.py", line 200, in run_filters
    doc = load(input_stream=input_stream)
  File "D:\software\Anaconda3\lib\site-packages\panflute\io.py", line 58, in load
    doc = json.load(input_stream, object_hook=from_json)
  File "D:\software\Anaconda3\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "D:\software\Anaconda3\lib\json\__init__.py", line 370, in loads
    return cls(**kw).decode(s)
  File "D:\software\Anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\software\Anaconda3\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "D:\software\Anaconda3\lib\site-packages\panflute\elements.py", line 1384, in from_json
    raise NotImplementedError(f'Unknown tag: {tag}')
NotImplementedError: Unknown tag: Caption
Error running filter D:\university\master\courses\md2report\backend\filters\general.py:
Filter returned error status 1

应该是 --filter 过滤器这个general.py python文件有问题吧?
pandoc 输出的参数是

pandoc -s --toc D:\university\master\courses\md2report\backend\test\test_case\5.2数据结构实验报告.md.validated.md --reference-doc D:\university\master\courses\md2report\backend\reference-docs\HUST.docx --filter D:\university\master\courses\md2report\backend\filters\general.py -o D:\university\master\courses\md2report\backend\output.docx

去掉 --filter 参数就可以用,但是缺少了添加摘要、修改图片格式等操作

无法运行,提示/app/filters/general.py: createProcess: posix_spawnp: invalid argument (Exec format error)

构建docker后进入docker,运行:

python md2report.py -i test/test_case/5.2数据结构实验报告.md

报错:

Error running filter /app/filters/general.py:
/app/filters/general.py: createProcess: posix_spawnp: invalid argument (Exec format error)
Traceback (most recent call last):
  File "/app/md2report.py", line 126, in <module>
    convert_md_to_docx(conf)
  File "/app/md2report.py", line 86, in convert_md_to_docx
    handler_map[h](str(output_path.absolute()))
  File "/app/docx_handler/hust.py", line 102, in process_hust_docx
    doc: TDocument = Document(filename)
                     ^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/docx/api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part
                    ^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/docx/opc/package.py", line 128, in open
    pkg_reader = PackageReader.from_file(pkg_file)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/docx/opc/pkgreader.py", line 32, in from_file
    phys_reader = PhysPkgReader(pkg_file)
                  ^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/docx/opc/phys_pkg.py", line 30, in __new__
    raise PackageNotFoundError(
docx.opc.exceptions.PackageNotFoundError: Package not found at '/app/output.docx'

已尝试了release中的各个版本文件,除docker外,在wsl以及Arch linux中尝试部署CLI并运行也是同样报错:

general.py: createProcess: posix_spawnp: invalid argument (Exec format error)

WebUI 在点击生成按钮后无法下载并弹出“文件下载失败”的提示

image
浏览器:Chrome 107.0.5304.107
控制台输出错误:

{
    "message": "Request failed with status code 500",
    "name": "AxiosError",
    "stack": "AxiosError: Request failed with status code 500\n    at https://md2report.hust.online/static/js/main.cd40c563.js:2:160117\n    at XMLHttpRequest.d (https://md2report.hust.online/static/js/main.cd40c563.js:2:160262)",
    "config": {
        "transitional": {
            "silentJSONParsing": true,
            "forcedJSONParsing": true,
            "clarifyTimeoutError": false
        },
        "transformRequest": [
            null
        ],
        "transformResponse": [
            null
        ],
        "timeout": 0,
        "xsrfCookieName": "XSRF-TOKEN",
        "xsrfHeaderName": "X-XSRF-TOKEN",
        "maxContentLength": -1,
        "maxBodyLength": -1,
        "env": {},
        "headers": {
            "Accept": "application/json, text/plain, */*"
        },
        "responseType": "blob",
        "method": "get",
        "url": "/api/tasks/0a90c74d-2c2d-41c6-8c18-03f1cb2e8a7c"
    },
    "code": "ERR_BAD_RESPONSE",
    "status": 500
}

windows下文件编码问题(需要添加俩encoding)

md2report.py的第25行和第48行添加, encoding='utf-8'

第25行不添加会报错:

发生异常: UnicodeDecodeError
'gbk' codec can't decode byte 0x8c in position 56: illegal multibyte sequence

第48行不添加会报错:

.md is not UTF-8 encoded: falling back to latin1.[WARNING] Could not fetch resource 碌梅脫脙鹿脴脧碌.png: replacing image with description[WARNING] Could not fetch resource 鲁玫麓脦脢鹿脫脙.png: replacing image with description[WARNING] Could not fetch resource 鲁玫麓脦脢鹿脫脙-2.png: replacing image with description
[WARNING] Could not fetch resource 卤脿脗毛.png: replacing image with description
[WARNING] Could not fetch resource 卤脿脗毛-2.png: replacing image with description
[WARNING] Could not fetch resource 卤脿脗毛-3.png: replacing image with description
[WARNING] Could not fetch resource 陆芒脗毛.png: replacing image with description[WARNING] Could not fetch resource 陆芒脗毛-2.png: replacing image with description
[WARNING] Could not fetch resource 脢盲鲁枚卤脿脗毛.png: replacing image with description
[WARNING] Could not fetch resource 脢盲鲁枚脢梅.png: replacing image with description
[WARNING] Could not fetch resource 脢盲鲁枚脢梅-2.png: replacing image with description

添加后能正常运行。

Great job on the development of this project, keep up the good work!

转换测试用例时报错:pandoc: Data.Binary.Get.runGet at position 4: Did not find end of central directory signature

运行的命令:

git pull
cd backend
poetry install
poetry shell
python md2report.py -i test/test_case/5.2数据结构实验报告.md

完整的错误信息:

(md2report-py3.10) [1] 03:37 Syameimaru-Aya (master) ~/sr/hu/md2report/backend
0 python md2report.py -i test/test_case/5.2数据结构实验报告.md
Please give me star if this application helps you!
如果这个应用有帮助到你,请给我点一个 star!
https://github.com/woolen-sheep/md2report
pandoc: Data.Binary.Get.runGet at position 4: Did not find end of central directory signature
CallStack (from HasCallStack):
  error, called at libraries/binary/src/Data/Binary/Get.hs:351:5 in binary-0.8.8.0:Data.Binary.Get
Traceback (most recent call last):
  File "/home/jyi/src/hust/md2report/backend/md2report.py", line 141, in <module>
    convert_md_to_docx(conf)
  File "/home/jyi/src/hust/md2report/backend/md2report.py", line 91, in convert_md_to_docx
    handler_map[handler_name](str(output_path.absolute()), **params)
  File "/home/jyi/src/hust/md2report/backend/docx_handler/hust.py", line 102, in process_hust_docx
    doc: TDocument = Document(filename)
  File "/home/jyi/.cache/pypoetry/virtualenvs/md2report-5grrADQi-py3.10/lib/python3.10/site-packages/docx/api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part
  File "/home/jyi/.cache/pypoetry/virtualenvs/md2report-5grrADQi-py3.10/lib/python3.10/site-packages/docx/opc/package.py", line 128, in open
    pkg_reader = PackageReader.from_file(pkg_file)
  File "/home/jyi/.cache/pypoetry/virtualenvs/md2report-5grrADQi-py3.10/lib/python3.10/site-packages/docx/opc/pkgreader.py", line 32, in from_file
    phys_reader = PhysPkgReader(pkg_file)
  File "/home/jyi/.cache/pypoetry/virtualenvs/md2report-5grrADQi-py3.10/lib/python3.10/site-packages/docx/opc/phys_pkg.py", line 30, in __new__
    raise PackageNotFoundError(
docx.opc.exceptions.PackageNotFoundError: Package not found at '/home/jyi/src/hust/md2report/backend/output.docx'

各个程序的版本:

  • python: 3.10.9
  • pandoc: 2.17.1.1
  • poetry: 1.2.2
(md2report-py3.10) 03:46 Syameimaru-Aya (master) ~/sr/hu/md2report/backend
0 python --version
Python 3.10.9
(md2report-py3.10) 03:46 Syameimaru-Aya (master) ~/sr/hu/md2report/backend
0 pandoc --version
pandoc 2.17.1.1
Compiled with pandoc-types 1.22.2.1, texmath 0.12.4, skylighting 0.12.3.1,
citeproc 0.6.0.1, ipynb 0.2
User data directory: /home/jyi/.local/share/pandoc
Copyright (C) 2006-2022 John MacFarlane. Web:  https://pandoc.org
This is free software; see the source for copying conditions. There is no
warranty, not even for merchantability or fitness for a particular purpose.
(md2report-py3.10) 03:46 Syameimaru-Aya (master) ~/sr/hu/md2report/backend
0 poetry --version
Poetry (version 1.2.2)

发行版:Debian GNU/Linux bookworm/sid

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.