Skip to content

Instantly share code, notes, and snippets.

@slhck
Last active June 5, 2024 09:36
Show Gist options
  • Save slhck/768892d27e0f240596032c188639eba3 to your computer and use it in GitHub Desktop.
Save slhck/768892d27e0f240596032c188639eba3 to your computer and use it in GitHub Desktop.
Export Trello cards to Markdown
#!/usr/bin/env python3
import argparse
import json
def main():
parser = argparse.ArgumentParser(description="Convert Trello JSON to Markdown")
parser.add_argument("file", type=str, help="Trello JSON file")
parser.add_argument("-l", "--list", help="Filter list name to convert")
parser.add_argument(
"-d",
"--docx",
action="store_true",
help="Convert to DOCX instead, requires -o/--output to be set",
)
parser.add_argument(
"-o",
"--output",
type=str,
help="Output file name. If unset, the output will be printed to the stdout.",
)
args = parser.parse_args()
if args.docx and not args.output:
parser.error("The --docx option requires the -o/--output option to be set.")
# Load the JSON data
with open(args.file) as file:
data = json.load(file)
# Load lists and IDs
lists: dict[str, str] = {}
for item in data["lists"]:
lists[item["id"]] = item["name"]
markdown_content = ""
for card in data["cards"]:
# Filter list name
if args.list and lists[card["idList"]] != args.list:
continue
# Add card name
markdown_content += f"# {card['name']}\n\n"
# Add card due date
if card["due"]:
markdown_content += f"**Due: {card['due']}**\n\n"
# # Add card URL
# markdown_content += f"[Trello URL]({card['shortUrl']})\n\n"
# # Add card labels
# if card["labels"]:
# markdown_content += "**Labels**: "
# markdown_content += ", ".join([label["name"] for label in card["labels"]])
# markdown_content += "\n\n"
# Add card description
if card["desc"]:
markdown_content += f"{card['desc']}\n\n"
# # Add card attachments
# if card["attachments"]:
# markdown_content += "Attachments:\n\n"
# for attachment in card["attachments"]:
# markdown_content += f"- [{attachment['name']}]({attachment['url']})\n\n"
# # Add card comments
# if card["actions"]:
# markdown_content += "Comments:\n\n"
# for action in card["actions"]:
# if action["type"] == "commentCard":
# markdown_content += f"- {action['data']['text']}\n\n"
# Add card checklist items
if card.get("checklists"):
markdown_content += "Checklist:\n\n"
for checklist in card["checklists"]:
markdown_content += f"- {checklist['name']}\n\n"
for item in checklist["checkItems"]:
markdown_content += f" - {'[x]' if item['state'] == 'complete' else '[ ]'} {item['name']}\n\n"
# Add a new line
markdown_content += "\n"
# Save the markdown content to a file
if args.output:
# If Markdown, save the content to a file
with open(args.output, "w") as file:
file.write(markdown_content)
# If .docx, convert the Markdown content to DOCX using pandoc by feeding stdin
if args.docx:
import subprocess
try:
subprocess.run(
[
"pandoc",
"-f",
"markdown",
"-o",
args.output.replace(".md", ".docx"),
],
input=markdown_content,
text=True,
check=True,
)
except FileNotFoundError:
print("Pandoc is not installed. Please install Pandoc first.")
except Exception as e:
print(f"An error occurred: {e}")
else:
print(f"File saved as {args.output.replace('.md', '.docx')}")
else:
print(markdown_content)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment