แปลงเอกสารเป็น Markdown

Markdown เป็นรูปแบบยอดนิยมที่ใช้ในการมาร์กอัปข้อความและแปลงเป็น HTML, PDF, DOCX หรือรูปแบบอื่นๆ เพิ่มเติม นักพัฒนาหลายคนเลือกรูปแบบนี้สำหรับการเขียนเอกสาร เตรียมบทความสำหรับการตีพิมพ์ในบล็อก อธิบายโครงการ และอื่นๆ

Markdown ได้รับความนิยมอย่างมากเนื่องจากง่ายต่อการใช้งานกับรูปแบบนี้ และสามารถแปลงเป็นรูปแบบอื่นได้อย่างง่ายดาย ด้วยเหตุนี้ Aspose.Words จึงมีความสามารถในการแปลงเอกสารใน รูปแบบการโหลดที่รองรับ เป็น Markdown และในทางกลับกัน Aspose.Words ยังรองรับ บันทึกรูปแบบ ยอดนิยมส่วนใหญ่อีกด้วย

ขณะนี้ฟังก์ชันการทำงานกับรูปแบบ Markdown กำลังได้รับการพัฒนาอย่างแข็งขันเพื่อให้คุณมีโอกาสมากขึ้นในการทำงานกับเอกสารที่สะดวกและสบาย

แปลงเอกสาร

หากต้องการแปลงเอกสารเป็น Markdown คุณเพียงแค่ต้องโหลดเอกสารในรูปแบบที่รองรับหรือสร้างเอกสารใหม่โดยทางโปรแกรม จากนั้นคุณจะต้องบันทึกเอกสารเป็นรูปแบบ Markdown

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีแปลง DOCX เป็น Markdown:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.writeln("Some text!")
doc.save(docs_base.artifacts_dir + "BaseConversions.docx_to_markdown.md")

คุณยังสามารถระบุโฟลเดอร์จริงที่คุณต้องการบันทึกรูปภาพเมื่อส่งออกเอกสารเป็นรูปแบบ Markdown ตามค่าเริ่มต้น Aspose.Words จะบันทึกรูปภาพในโฟลเดอร์เดียวกับที่บันทึกไฟล์เอกสาร แต่คุณสามารถแทนที่ลักษณะการทำงานนี้ได้โดยใช้คุณสมบัติ images_folder

การระบุโฟลเดอร์ผ่าน images_folder ยังมีประโยชน์หากคุณบันทึกเอกสารลงในสตรีมและ Aspose.Words ไม่มีโฟลเดอร์สำหรับบันทึกรูปภาพ

หากไม่มี images_folder ที่ระบุ images_folder จะถูกสร้างขึ้นโดยอัตโนมัติ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการระบุโฟลเดอร์สำหรับรูปภาพเมื่อบันทึกเอกสารลงในสตรีม:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Image bullet points.docx")
saveOptions = aw.saving.MarkdownSaveOptions()
saveOptions.images_folder = docs_base.artifacts_dir + "Images"
doc.save(docs_base.artifacts_dir + "WorkingWithMarkdownSaveOptions.set_images_folder.md", saveOptions)

ระบุตัวเลือกการบันทึกเมื่อแปลงเป็น Markdown

Aspose.Words ให้ความสามารถในการใช้คลาส MarkdownSaveOptions เพื่อทำงานกับตัวเลือกขั้นสูงเมื่อบันทึกเอกสารเป็นรูปแบบ Markdown คุณสมบัติส่วนใหญ่จะสืบทอดหรือโอเวอร์โหลดคุณสมบัติที่มีอยู่แล้วในคลาส aspose.words.saving อื่น นอกจากนั้น ยังมีการเพิ่มคุณสมบัติจำนวนหนึ่งที่เฉพาะเจาะจงสำหรับรูปแบบ Markdown อีกด้วย ตัวอย่างเช่น คุณสมบัติ table_content_alignment เพื่อควบคุมการจัดแนวเนื้อหาในตาราง หรือ images_folder เพื่อควบคุมตำแหน่งที่จะบันทึกรูปภาพเมื่อแปลงเอกสารเป็นรูปแบบ Markdown

คุณสมบัติ Markdown ที่รองรับ

ปัจจุบัน Aspose.Words รองรับฟีเจอร์ Markdown ต่อไปนี้ ซึ่งส่วนใหญ่เป็นไปตามข้อกำหนด CommonMark ใน Aspose.Words API และแสดงเป็นรูปแบบที่เหมาะสมหรือการจัดรูปแบบโดยตรง:

  • ส่วนหัวคือย่อหน้าที่มีรูปแบบหัวข้อ 1 – หัวข้อ 6
  • Blockquotes คือย่อหน้าที่มี “Quote” อยู่ในชื่อสไตล์
  • IndentedCode คือย่อหน้าที่มี “IndentedCode” ในชื่อสไตล์
  • FencedCode คือย่อหน้าที่มี “FencedCode” ในชื่อสไตล์
  • InlineCode ทำงานด้วย “InlineCode” ในชื่อรูปแบบ Font
  • กฎแนวนอนคือย่อหน้าที่มีรูปร่าง HorizontalRule
  • เน้นตัวหนา
  • เน้นตัวเอียง
  • การจัดรูปแบบ StrikeThrough
  • รายการเป็นย่อหน้าที่มีหมายเลขหรือหัวข้อย่อย
  • ตารางแสดงด้วยคลาส Table
  • ลิงก์จะแสดงเป็นคลาส FieldHyperlink

ตัวอย่างต่อไปนี้แสดงวิธีสร้างเอกสารด้วยสไตล์บางอย่างและบันทึกลงใน Markdown:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# Specify the "Heading 1" style for the paragraph.
builder.paragraph_format.style_name = "Heading 1"
builder.writeln("Heading 1")
# Reset styles from the previous paragraph to not combine styles between paragraphs.
builder.paragraph_format.style_name = "Normal"
# Insert horizontal rule.
builder.insert_horizontal_rule()
# Specify the ordered list.
builder.insert_paragraph()
builder.list_format.apply_number_default()
# Specify the Italic emphasis for the text.
builder.font.italic = True
builder.writeln("Italic Text")
builder.font.italic = False
# Specify the Bold emphasis for the text.
builder.font.bold = True
builder.writeln("Bold Text")
builder.font.bold = False
# Specify the StrikeThrough emphasis for the text.
builder.font.strike_through = True
builder.writeln("StrikeThrough Text")
builder.font.strike_through = False
# Stop paragraphs numbering.
builder.list_format.remove_numbers()
# Specify the "Quote" style for the paragraph.
builder.paragraph_format.style_name = "Quote"
builder.writeln("A Quote block")
# Specify nesting Quote.
nestedQuote = doc.styles.add(aw.StyleType.PARAGRAPH, "Quote1")
nestedQuote.base_style_name = "Quote"
builder.paragraph_format.style_name = "Quote1"
builder.writeln("A nested Quote block")
# Reset paragraph style to Normal to stop Quote blocks.
builder.paragraph_format.style_name = "Normal"
# Specify a Hyperlink for the desired text.
builder.font.bold = True
# Note, the text of hyperlink can be emphasized.
builder.insert_hyperlink("Aspose", "https:#www.aspose.com", False)
builder.font.bold = False
# Insert a simple table.
builder.start_table()
builder.insert_cell()
builder.write("Cell1")
builder.insert_cell()
builder.write("Cell2")
builder.end_table()
# Save your document as a Markdown file.
doc.save(docs_base.artifacts_dir + "WorkingWithMarkdown.create_markdown_document.md")

ผลลัพธ์ของตัวอย่างโค้ดนี้แสดงอยู่ด้านล่าง

markdown-example-aspose-words-net

เคล็ดลับที่เป็นประโยชน์

มีความแตกต่างและกรณีที่น่าสนใจหลายประการโดยได้เรียนรู้ว่าไฟล์ Markdown ใดที่คุณสามารถทำงานกับไฟล์ Markdown ได้อย่างยืดหยุ่นและสะดวกยิ่งขึ้น ตัวอย่างเช่น มีความสามารถในการใช้งาน:

  • SetextHeading ที่ให้คุณสร้างส่วนหัวแบบหลายบรรทัดใน Markdown ในขณะที่ส่วนหัวปกติใน Markdown ต้องเป็นบรรทัดเดียวเท่านั้น SetextHeading ขึ้นอยู่กับสไตล์ “Heading N” และระดับของมันสามารถเป็นได้เพียง 1 หรือ 2 เท่านั้น ถ้า N ใน “Heading N” มากกว่าหรือเท่ากับ 2 ดังนั้น SetextHeading ที่สอดคล้องกันจะขึ้นอยู่กับ “Heading 2” มิฉะนั้นจะอยู่บน “หัวข้อที่ 1”.
  • เครื่องหมายที่แตกต่างกันสำหรับระดับแรกของรายการสัญลักษณ์แสดงหัวข้อย่อย ("-", “+” หรือ “*” เครื่องหมายเริ่มต้นคือ “-") และการกำหนดหมายเลขประเภทต่างๆ สำหรับรายการที่เรียงลำดับ (”." หรือ “)” เครื่องหมายเริ่มต้นคือ “.")