In daily work, it is inevitable that some small tips are needed to solve problems encountered in work. Today's article will recommend a convenient and fast trick to convert Office (doc/docx/ppt/pptx/xlsx) files into PDF files in batches or single files. However, before doing specific operations, you need to install Office on your PC, and then use Python's win32com package to implement the conversion of Office files.
Install win32com
Before actual combat, you need to install Python win32com. The detailed installation steps are as follows:
Use the pip command to install
pip install pywin32
If we encounter an installation error, we can update the cloud through python -m pip install -upgrade pip and then install it:
python -m pip install --upgrade pip
Download offline installation package to install
If the pip command is not installed successfully, you can download the offline package to install. The steps are as follows: First, select the corresponding Python version on the official website to download the offline package:/projects/pywin32/files/pywin32/Build%20221/After downloading, just install it in 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'] # Support conversion file types self._filename_list = list() #List files self._export_folder = (('.'), 'file_server/pdfconver') if not (self._export_folder): (self._export_folder) self._enumerate_filename(pathname) def _enumerate_filename(self, pathname): ''' Read all file names ''' full_pathname = (pathname) if (full_pathname): if self._is_legal_postfix(full_pathname): self._filename_list.append(full_pathname) else: raise TypeError('document {} Illegal suffix name!仅支持如下document类型:{}。'.format(pathname, '、'.join(self._handle_postfix))) elif (full_pathname): for relpath, _, files in (full_pathname): for name in files: filename = (full_pathname, relpath, name) if self._is_legal_postfix(filename): self._filename_list.append((filename)) else: raise TypeError('document/document夹 {} Not existent or illegal!'.format(pathname)) def _is_legal_postfix(self, filename): return ('.')[-1].lower() in self._handle_postfix and not (filename).startswith( '~') def run_conver(self): print('The number of files to be converted is:', len(self._filename_list)) for filename in self._filename_list: postfix = ('.')[-1].lower() funcCall = getattr(self, postfix) print('Original file:', filename) funcCall(filename) print('The conversion is complete! ')
Convert doc/docx to PDF
The code to convert doc/docx to PDF is as follows:
def doc(self, filename): name = (filename).split('.')[0] + '.pdf' exportfile = (self._export_folder, name) print('Save PDF file:', exportfile) ('{00020905-0000-0000-C000-000000000046}', 0, 8, 4) () w = Dispatch("") () # Add to prevent CoInitialize from loading doc = (filename) (exportfile, , Item=, CreateBookmarks=) () def docx(self, filename): (filename)
ppt/pptx to PDF
Part of the code to convert ppt/pptx to PDF is as follows:
def ppt(self, filename): name = (filename).split('.')[0] + '.pdf' exportfile = (self._export_folder, name) ('{00020905-0000-0000-C000-000000000046}', 0, 8, 4) () p = Dispatch("") () ppt = (filename, False, False, False) (exportfile, 2, PrintRange=None) print('Save PDF file:', exportfile) () def pptx(self, filename): (filename)
xls/xlsx to PDF
def xls(self, filename): name = (filename).split('.')[0] + '.pdf' exportfile = (self._export_folder, name) () xlApp = DispatchEx("") () = False = 0 books = (filename, False) (0, exportfile) (False) print('Save PDF file:', exportfile) () def xlsx(self, filename): (filename)
Perform logical transformations
if __name__ == "__main__": # Support batch import of folders #folder = 'tmp' #pathname = (('.'), folder) # also supports conversion of individual files pathname = "G:/python_study/" pdfConverter = PDFConverter(pathname) pdfConverter.run_conver()
Summarize
Today's article is mainly about the use of Python practical gadgets. I hope it will be helpful to everyone. The next issue will explain how to download and convert files through the file server through an interface. Stay tuned. . . So Today's Little Tip Have you recommended it?
Sample code
/JustDoPython/python-examples/tree/master/chaoxi/FilesToPDF
The above is the detailed content of how to convert office files into PDF in python. For more information about python Office files to PDF, please follow my other related articles!