Created
February 15, 2025 12:21
-
-
Save robes7/3899fd3326032f84ade6ab5408a142e9 to your computer and use it in GitHub Desktop.
PDF to WORD Converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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