Rumah >pembangunan bahagian belakang >Tutorial Python >Bagaimana untuk menggunakan dua baris kod Python untuk menukar pdf kepada perkataan
pip install --index https://pypi.mirrors.ustc.edu.cn/simple/ python-office
[INFO] [2/4] Menganalisis dokumen...[AMARAN] cap waktu yang 'dibuat' kelihatan sangat rendah ; berkenaan sebagai cap waktu unix
补充
[AMARAN] cap waktu 'diubah suai' kelihatan sangat rendah; berkenaan sebagai cap waktu unix
[AMARAN] cap waktu 'dicipta' nampaknya sangat rendah; berkenaan sebagai cap waktu unix
[AMARAN] cap waktu 'diubah suai' kelihatan sangat rendah; berkenaan sebagai cap waktu unix
[INFO] [3/4] Menghuraikan halaman...
[INFO] (1/9) Halaman 1
[INFO] (2/9) Halaman 2
[ INFO] (3/9) Halaman 3
[INFO] (4/9) Halaman 4
[INFO] (5/9) Halaman 5
[INFO] (6/9) Halaman 6
[INFO] (7/9) Halaman 7
[INFO] (8/9) Halaman 8
[INFO] (9/9) Halaman 9
[INFO] [4/4] Mencipta halaman ...
[INFO] (1/9) Halaman 1
[INFO] (2/9) Halaman 2
[INFO] (3/9) Halaman 3
[INFO] (4 /9) Halaman 4
[INFO] (5/9) Halaman 5
[INFO] (6/9) Halaman 6
[INFO] (7/9) Halaman 7
[INFO] (8/9) Halaman 8
[INFO] (9/9) Halaman 9
[INFO] Ditamatkan dalam 1.30s.
Proses selesai dengan kod keluar 0
方法一:
import office office.pdf.pdf2docx(file_path = 'test.pdf')方法二:加密过的PDF转word
import os from configparser import ConfigParser from io import StringIO from io import open from concurrent.futures import ProcessPoolExecutor from pdfminer.pdfinterp import PDFResourceManager from pdfminer.pdfinterp import process_pdf from pdfminer.converter import TextConverter from pdfminer.layout import LAParams from docx import Document def read_from_pdf(file_path): with open(file_path, 'rb') as file: resource_manager = PDFResourceManager() return_str = StringIO() lap_params = LAParams() device = TextConverter( resource_manager, return_str, laparams=lap_params) process_pdf(resource_manager, device, file) device.close() content = return_str.getvalue() return_str.close() return content def save_text_to_word(content, file_path): doc = Document() for line in content.split('\n'): paragraph = doc.add_paragraph() paragraph.add_run(remove_control_characters(line)) doc.save(file_path) def remove_control_characters(content): mpa = dict.fromkeys(range(32)) return content.translate(mpa) def pdf_to_word(pdf_file_path, word_file_path): content = read_from_pdf(pdf_file_path) save_text_to_word(content, word_file_path) def main(): config_parser = ConfigParser() config_parser.read('config.cfg') config = config_parser['default'] tasks = [] with ProcessPoolExecutor(max_workers=int(config['max_worker'])) as executor: for file in os.listdir(config['pdf_folder']): extension_name = os.path.splitext(file)[1] if extension_name != '.pdf': continue file_name = os.path.splitext(file)[0] pdf_file = config['pdf_folder'] + '/' + file word_file = config['word_folder'] + '/' + file_name + '.docx' print('正在处理: ', file) result = executor.submit(pdf_to_word, pdf_file, word_file) tasks.append(result) while True: exit_flag = True for task in tasks: if not task.done(): exit_flag = False if exit_flag: print('完成') exit(0) if __name__ == '__main__': main()
Atas ialah kandungan terperinci Bagaimana untuk menggunakan dua baris kod Python untuk menukar pdf kepada perkataan. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!