Python Small skills Office File transfer PDF

writing | The tides

source :Python technology 「ID: pythonall」

Python Small skills Office File transfer PDF

In normal work , It's hard to avoid needing some Small Tip To solve problems at work , Today's article gives you a quick and convenient tips for Amway , take Office(doc/docx/ppt/pptx/xls/xlsx) File batch or single file conversion to PDF file . But before doing the specific operation, we need to PC Install well Office, recycling Python Of win32com Package to achieve Office File conversion operation .

install win32com

Before the actual battle , Need to install Python Of win32com, The detailed installation steps are as follows :
Use pip Command to install

pip install pywin32

If we encounter an installation error , Can pass python -m pip install --upgrade pip Update the cloud and then install it :

python -m pip install --upgrade pip 

Download the offline installation package to install

If pip If the command is not installed successfully, you can download the offline package for installation , The method steps are as follows : First, select the corresponding Python Version download offline package :https://sourceforge.net/projects/pywin32/files/pywin32/Build%20221/ After downloading, you can install it like a fool .

File conversion logic

The detailed code is as follows :

class PDFConverter:
def __init__(self, pathname, export='.'):
self._handle_postfix = ['doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx'] # File types that support conversion
self._filename_list = list() # Lists the files
self._export_folder = os.path.join(os.path.abspath('.'), 'file_server/pdfconver')
if not os.path.exists(self._export_folder):
os.mkdir(self._export_folder)
self._enumerate_filename(pathname)
def _enumerate_filename(self, pathname):
'''
Read all file names
'''
full_pathname = os.path.abspath(pathname)
if os.path.isfile(full_pathname):
if self._is_legal_postfix(full_pathname):
self._filename_list.append(full_pathname)
else:
raise TypeError(' file {} Illegal suffix ! Only the following file types are supported :{}.'.format(pathname, '、'.join(self._handle_postfix)))
elif os.path.isdir(full_pathname):
for relpath, _, files in os.walk(full_pathname):
for name in files:
filename = os.path.join(full_pathname, relpath, name)
if self._is_legal_postfix(filename):
self._filename_list.append(os.path.join(filename))
else:
raise TypeError(' file / Folder {} Nonexistence or illegality !'.format(pathname))
def _is_legal_postfix(self, filename):
return filename.split('.')[-1].lower() in self._handle_postfix and not os.path.basename(filename).startswith(
'~')
def run_conver(self):
print(' The number of files that need to be converted is :', len(self._filename_list))
for filename in self._filename_list:
postfix = filename.split('.')[-1].lower()
funcCall = getattr(self, postfix)
print(' The original document :', filename)
funcCall(filename)
print(' convert network !')

doc/docx Convert to PDF

doc/docx Convert to PDF Part of the code is shown below :


def doc(self, filename):
name = os.path.basename(filename).split('.')[0] + '.pdf'
exportfile = os.path.join(self._export_folder, name)
print(' preservation PDF file :', exportfile)
gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)
pythoncom.CoInitialize()
w = Dispatch("Word.Application")
pythoncom.CoInitialize() # Plus prevention CoInitialize Not loaded
doc = w.Documents.Open(filename)
doc.ExportAsFixedFormat(exportfile, constants.wdExportFormatPDF,
Item=constants.wdExportDocumentWithMarkup,
CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
w.Quit(constants.wdDoNotSaveChanges)
def docx(self, filename):
self.doc(filename)

ppt/pptx Convert to PDF

ppt/pptx Convert to PDF Part of the code is as follows :


def ppt(self, filename):
name = os.path.basename(filename).split('.')[0] + '.pdf'
exportfile = os.path.join(self._export_folder, name)
gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)
pythoncom.CoInitialize()
p = Dispatch("PowerPoint.Application")
pythoncom.CoInitialize()
ppt = p.Presentations.Open(filename, False, False, False)
ppt.ExportAsFixedFormat(exportfile, 2, PrintRange=None)
print(' preservation PDF file :', exportfile)
p.Quit()
def pptx(self, filename):
self.ppt(filename)

xls/xlsx Convert to PDF

 def xls(self, filename):
name = os.path.basename(filename).split('.')[0] + '.pdf'
exportfile = os.path.join(self._export_folder, name)
pythoncom.CoInitialize()
xlApp = DispatchEx("Excel.Application")
pythoncom.CoInitialize()
xlApp.Visible = False
xlApp.DisplayAlerts = 0
books = xlApp.Workbooks.Open(filename, False)
books.ExportAsFixedFormat(0, exportfile)
books.Close(False)
print(' preservation PDF file :', exportfile)
xlApp.Quit()
def xlsx(self, filename):
self.xls(filename) 

Perform transformation logic

if __name__ == "__main__":
# Batch import of folder is supported
#folder = 'tmp'
#pathname = os.path.join(os.path.abspath('.'), folder)
# It also supports the conversion of a single file
pathname = "G:/python_study/test.doc"
pdfConverter = PDFConverter(pathname)
pdfConverter.run_conver()

summary

Today's article is mainly about Python The use of practical tools , I hope that's helpful , The next issue will explain how to download and convert files through the file server through the interface , Coming soon ...

So Today's little Tip Are you here at Amway ?

 Sample code Python Small skills Office File transfer PDF