1.word转pdf
from win32com.client import Dispatch,constants,gencache #pip install pywin32
docx_path='d:/python_test/word/test.docx' #源文件
pdf_path='d:/python_test/word/test.pdf' #转换后的文件
gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}',0,8,4)#这串数字指向word
wd=Dispatch('Word.Application') #启用word
doc=wd.Documents.Open(docx_path,ReadOnly=1) #打开word文档
doc.ExportAsFixedFormat(pdf_path,constants.wdExportFormatPDF,
Item=constants.wdExportDocumentWithMarkup,
CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
wd.Quit(constants.wdDoNotSaveChanges)
2.pdf读取
from io import StringIO
from xmlrpc.server import resolve_dotted_attribute
from pdfminer.pdfinterp import PDFResourceManager,process_pdf #pip install pdfminer3k
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
pdf_file=open('d:/python_test/word/test.pdf','rb')
rsrcmgr=PDFResourceManager()
retstr=StringIO()
laparams=LAParams()
device=TextConverter(rsrcmgr=rsrcmgr,outfp=retstr,laparams=laparams)
process_pdf(rsrcmgr=rsrcmgr,device=device,fp=pdf_file)
device.close()
content=retstr.getvalue()
retstr.close()
pdf_file.close()
print(content)