Converting several Markdown files into DOCX with pandoc

Joined
Feb 1, 2023
Messages
3
Reaction score
0
I have a folder called Project:

Project:

img
docs
README

The README is written in Markdown and looks like this:
document 1 document 1.1 document 2 document 2.1 document 2.1.1
The readme has links to the individual Markdown files. The file structure in the document folder looks like this:
Document 1 Document 2 document 11 Document 21 Document 211
The documents have images that come from the img folder and have cross references to each other e.g. Document 1 has a reference to Document 11.

How can I use a pandoc command to convert the documents as they appear in order in the README to a DOCX file, with the corresponding images and the cross references in the respective Markdown files. Is there a suitable script with pandoc for this?

I had it once with the pandoc command:

for /r "." %i in (*.md) do pandoc -o "%~i.docx" "%~i"

All files in the docs folder were converted to a DOCX file and merged, but the order was completely wrong and the cross references were to the Markdown files from the docs folder and not from the created DOCX file.
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
You can try using a script that reads the order of the documents as specified in the README file and converts them to DOCX files in that order, including the corresponding images and cross references.

Here's a bash script try it

Bash:
#!/bin/bash

# Read the order of the documents from the README file
docs_order=($(grep -Eo 'document [0-9]+((\.[0-9]+)*)' README))

# Convert each document in the specified order
for doc in "${docs_order[@]}"; do
  filename="docs/${doc//.}.md"
  pandoc -s -o "${filename%.md}.docx" "$filename"
done

This script assumes that the structure of the documents and the img folder is as described in your question and that the order of the documents in the README file corresponds to the desired order in the resulting DOCX file.
 
Joined
Feb 1, 2023
Messages
3
Reaction score
0
Thanks for the suggestion. I tried the code but I don't get any output.
I put the script in the folder of the project and then run it. But nothing happened and no docx file was created.
If it helps, my README file looks like this:
# xxxxxxxx

## Content

## 1. [xxxxxxxx](docs/1.md)
## 1.1 [xxxxxxxx](docs/11.md)
## 2. [xxxxxxxx](docs/2.md)
### 2.1 [xxxxxxxx](docs/21.md)
### 2.2 [xxxxxxxx](docs/22.md)
#### 2.2.1 [xxxxxxxx](docs/221.md)
#### 2.2.2 [xxxxxxxx](docs/222.md)
### 2.3 [xxxxxxxx](docs/23.md)
### 2.4. [xxxxxxxx](docs/24.md)
### 2.5. [xxxxxxxx](docs/25.md)
### 2.6. [xxxxxxxx](docs/26.md)
## 3. [xxxxxxxx](docs/3.md)
### 3.1. [xxxxxxxx](docs/31.md)
#### 3.1.1 [xxxxxxxx](docs/311.md)
#### 3.1.2 [xxxxxxxx](docs/312.md)
#### 3.1.3 [xxxxxxxx](docs/313.md)
#### 3.1.4 [xxxxxxxx](docs/314.md)
### 3.2 [xxxxxxxx](docs/32.md)
#### 3.2.1 [xxxxxxxx](docs/321.md)
#### 3.2.2. [xxxxxxxx](docs/322.md)
### 3.3. [xxxxxxxx](docs/33.md)
#### 3.3.1. [xxxxxxxx](/docs/331.md)
#### 3.3.2. [xxxxxxxx](/docs/322.md)
## 4. [xxxxxxxx](/docs/4.md)
### 4.1 [xxxxxxxx](docs/41.md)
### 4.2 [xxxxxxxx](docs/42.md)
### 4.3 [xxxxxxxx](docs/43.md)
### 4.4 [xxxxxxxx](docs/44.md)
## 5. [xxxxxxxx](/docs/5.md)
## 6. [Exxxxxxxx](/docs/6.md)

## [xxxxxxxx](docs/a_shorts.en)


and the folder structure of the Project:

docs
img
README
script
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
Thanks for the suggestion. I tried the code but I don't get any output.
I put the script in the folder of the project and then run it. But nothing happened and no docx file was created.
If it helps, my README file looks like this:
# xxxxxxxx

## Content

## 1. [xxxxxxxx](docs/1.md)
## 1.1 [xxxxxxxx](docs/11.md)
## 2. [xxxxxxxx](docs/2.md)
### 2.1 [xxxxxxxx](docs/21.md)
### 2.2 [xxxxxxxx](docs/22.md)
#### 2.2.1 [xxxxxxxx](docs/221.md)
#### 2.2.2 [xxxxxxxx](docs/222.md)
### 2.3 [xxxxxxxx](docs/23.md)
### 2.4. [xxxxxxxx](docs/24.md)
### 2.5. [xxxxxxxx](docs/25.md)
### 2.6. [xxxxxxxx](docs/26.md)
## 3. [xxxxxxxx](docs/3.md)
### 3.1. [xxxxxxxx](docs/31.md)
#### 3.1.1 [xxxxxxxx](docs/311.md)
#### 3.1.2 [xxxxxxxx](docs/312.md)
#### 3.1.3 [xxxxxxxx](docs/313.md)
#### 3.1.4 [xxxxxxxx](docs/314.md)
### 3.2 [xxxxxxxx](docs/32.md)
#### 3.2.1 [xxxxxxxx](docs/321.md)
#### 3.2.2. [xxxxxxxx](docs/322.md)
### 3.3. [xxxxxxxx](docs/33.md)
#### 3.3.1. [xxxxxxxx](/docs/331.md)
#### 3.3.2. [xxxxxxxx](/docs/322.md)
## 4. [xxxxxxxx](/docs/4.md)
### 4.1 [xxxxxxxx](docs/41.md)
### 4.2 [xxxxxxxx](docs/42.md)
### 4.3 [xxxxxxxx](docs/43.md)
### 4.4 [xxxxxxxx](docs/44.md)
## 5. [xxxxxxxx](/docs/5.md)
## 6. [Exxxxxxxx](/docs/6.md)

## [xxxxxxxx](docs/a_shorts.en)


and the folder structure of the Project:

docs
img
README
script

It looks like you need to specify the TOC file in the pandoc command. Create a new file in the same directory as your docs folder and name it TOC.md. Add the headings from your README file to the TOC.md file. The file should look something like this:

Code:
## 1. [xxxxxxxx](docs/1.md)
## 1.1 [xxxxxxxx](docs/11.md)
## 2. [xxxxxxxx](docs/2.md)
### 2.1 [xxxxxxxx](docs/21.md)
### 2.2 [xxxxxxxx](docs/22.md)
#### 2.2.1 [xxxxxxxx](docs/221.md)
#### 2.2.2 [xxxxxxxx](docs/222.md)
### 2.3 [xxxxxxxx](docs/23.md)
### 2.4. [xxxxxxxx](docs/24.md)
### 2.5. [xxxxxxxx](docs/25.md)
### 2.6. [xxxxxxxx](docs/26.md)
## 3. [xxxxxxxx](docs/3.md)
### 3.1. [xxxxxxxx](docs/31.md)
#### 3.1.1 [xxxxxxxx](docs/311.md)
#### 3.1.2 [xxxxxxxx](docs/312.md)
#### 3.1.3 [xxxxxxxx](docs/313.md)
#### 3.1.4 [xxxxxxxx](docs/314.md)
### 3.2 [xxxxxxxx](docs/32.md)
#### 3.2.1 [xxxxxxxx](docs/321.md)
#### 3.2.2. [xxxxxxxx](docs/322.md)
### 3.3. [xxxxxxxx](docs/33.md)
#### 3.3.1. [xxxxxxxx](/docs/331.md)
#### 3.3.2. [xxxxxxxx](/docs/322.md)
## 4. [xxxxxxxx](/docs/4.md)
### 4.1 [xxxxxxxx](docs/41.md)
### 4.2 [xxxxxxxx](docs/42.md)
### 4.3 [xxxxxxxx](docs/43.md)
### 4.4 [xxxxxxxx](docs/44.md)
## 5. [xxxxxxxx](/docs/5.md)
## 6. [Exxxxxxxx](/docs/6.md)

## [xxxxxxxx](docs/a_shorts.en)



It looks like you need to specify the TOC file in the pandoc command. Create a new file in the same directory as your docs folder and name it TOC.md. Add the headings from your README file to the TOC.md file. The file should look something like this:
scss
## 1. [xxxxxxxx](docs/1.md)
## 1.1 [xxxxxxxx](docs/11.md)
## 2. [xxxxxxxx](docs/2.md)
### 2.1 [xxxxxxxx](docs/21.md)
### 2.2 [xxxxxxxx](docs/22.md)
#### 2.2.1 [xxxxxxxx](docs/221.md)
#### 2.2.2 [xxxxxxxx](docs/222.md)
### 2.3 [xxxxxxxx](docs/23.md)
### 2.4. [xxxxxxxx](docs/24.md)
### 2.5. [xxxxxxxx](docs/25.md)
### 2.6. [xxxxxxxx](docs/26.md)
## 3. [xxxxxxxx](docs/3.md)
### 3.1. [xxxxxxxx](docs/31.md)
#### 3.1.1 [xxxxxxxx](docs/311.md)
#### 3.1.2 [xxxxxxxx](docs/312.md)
#### 3.1.3 [xxxxxxxx](docs/313.md)
#### 3.1.4 [xxxxxxxx](docs/314.md)
### 3.2 [xxxxxxxx](docs/32.md)
#### 3.2.1 [xxxxxxxx](docs/321.md)
#### 3.2.2. [xxxxxxxx](docs/322.md)
### 3.3. [xxxxxxxx](docs/33.md)
#### 3.3.1. [xxxxxxxx](/docs/331.md)
#### 3.3.2. [xxxxxxxx](/docs/322.md)
## 4. [xxxxxxxx](/docs/4.md)
### 4.1 [xxxxxxxx](docs/41.md)
### 4.2 [xxxxxxxx](docs/42.md)
### 4.3 [xxxxxxxx](docs/43.md)
### 4.4 [xxxxxxxx](docs/44.md)
## 5. [xxxxxxxx](/docs/5.md)
## 6. [Exxxxxxxx](/docs/6.md)

## [xxxxxxxx](docs/a_shorts.en)

Then, run the following pandoc command: pandoc -s -o output.docx TOC.md

This will convert the markdown files listed in the TOC.md file to a single .docx file named output.docx.
 
Joined
Feb 1, 2023
Messages
3
Reaction score
0
I see Is there also a way to write a script that then takes the README or TOC.md file and then converts the referenced md files to docx and then merges them into a single DOCX file where the TOC serves as the table of contents?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,922
Messages
2,570,047
Members
46,475
Latest member
RacheleGri

Latest Threads

Top