파일 변환¶
파일을 PDF로¶
PyMuPDF 가 지원하는 문서 유형 은 Document.convert_to_pdf() 메서드를 사용하여 쉽게 PDF 로 변환할 수 있습니다. 이 메서드는 데이터 버퍼를 반환하며, 이를 PyMuPDF 에서 사용하여 새로운 PDF 를 생성할 수 있습니다.
예제
import pymupdf
xps = pymupdf.open("input.xps")
pdfbytes = xps.convert_to_pdf()
pdf = pymupdf.open("pdf", pdfbytes)
pdf.save("output.pdf")
PDF를 SVG로¶
기술적으로 SVG 파일은 다중 페이지를 지원하지 않으므로 각 페이지를 SVG로 내보내야 합니다.
페이지의 SVG 표현을 얻으려면 Page.get_svg_image() 메서드를 사용하세요.
예제
import pymupdf
doc = pymupdf.open("input.pdf")
page = doc[0]
# Convert page to SVG
svg_content = page.get_svg_image()
# Save to file
with open("output.svg", "w", encoding="utf-8") as f:
f.write(svg_content)
doc.close()
PDF를 Markdown으로¶
PyMuPDF4LLM API 를 활용하여 PDF를 Markdown 표현으로 변환할 수 있습니다.
예제
import pymupdf4llm
import pathlib
md_text = pymupdf4llm.to_markdown("test.pdf")
print(md_text)
pathlib.Path("4llm-output.md").write_bytes(md_text.encode())
PDF를 DOCX로¶
PyMuPDF 를 사용하여 PDF 에서 DOCX 형식으로 문서 변환을 제공하는 pdf2docx 라이브러리를 사용하세요.
예제
from pdf2docx import Converter
pdf_file = 'input.pdf'
docx_file = 'output.docx'
# convert pdf to docx
cv = Converter(pdf_file)
cv.convert(docx_file) # all pages by default
cv.close()
