生成一段python代码,实现pdf转word
时间: 2023-11-11 08:07:21 浏览: 67
Sure, here is an example code using the PyPDF2 library:
```python
import os
import PyPDF2
from docx import Document
def pdf_to_word(file_path):
# Open PDF file
pdf_file = open(file_path, 'rb')
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
# Create a new Word document
word_doc = Document()
# Loop through each page in the PDF
for page_num in range(pdf_reader.numPages):
page = pdf_reader.getPage(page_num)
# Extract the text from the PDF page and add it to the Word document
text = page.extractText()
word_doc.add_paragraph(text)
# Save the Word document with the same name as the PDF file
word_file_path = os.path.splitext(file_path)[0] + '.docx'
word_doc.save(word_file_path)
print(f'Successfully converted {file_path} to {word_file_path}')
# Example usage
pdf_to_word('example.pdf')
```
This code uses the PyPDF2 library to read in the PDF file and extract the text from each page. It then creates a new Word document using the `docx` library and adds the extracted text to the document. Finally, it saves the Word document with the same name as the original PDF file.
阅读全文