Skip to content

Instantly share code, notes, and snippets.

@robes7
Created February 15, 2025 12:21
Show Gist options
  • Select an option

  • Save robes7/3899fd3326032f84ade6ab5408a142e9 to your computer and use it in GitHub Desktop.

Select an option

Save robes7/3899fd3326032f84ade6ab5408a142e9 to your computer and use it in GitHub Desktop.
PDF to WORD Converter
import fitz # PyMuPDF
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
def extract_text_from_pdf(pdf_path, pages=None):
document = fitz.open(pdf_path)
text = []
if pages is None:
pages = range(document.page_count)
for page_num in pages:
page = document.load_page(page_num)
text.append(page.get_text("text"))
return text
def save_to_word(text, output_word_path):
doc = Document()
for page_text in text:
for line in page_text.split('\n'):
paragraph = doc.add_paragraph(line)
paragraph.alignment = WD_ALIGN_PARAGRAPH.LEFT
for run in paragraph.runs:
run.font.size = Pt(12)
doc.save(output_word_path)
# User configuration
pdf_path = 'x.pdf' # Path to your PDF file
output_word_path = 'output.docx' # Path to save the Word file
# Extract text from PDF
text = extract_text_from_pdf(pdf_path)
# Save to Word
save_to_word(text, output_word_path)
print(f"Data extracted and saved to {output_word_path}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment