A Python library for handling Microsoft Office documents, built with LibreOfficeKit.
- Convert Microsoft Office documents to PDF
-
Python 3.10 or higher
-
LibreOffice 7.2 or higher
sudo apt-get install -y libreoffice libreoffice-dev libreoffice-dev-doc
- The fonts used in the document must be installed on the system.For example,use Chinese, on Ubuntu, you can install the fonts by running the following command:
sudo apt-get install -y fonts-wqy-zenhei fonts-wqy-microhei xfonts-intl-chinese ttf-wqy-zenhei ttf-wqy-microhei language-pack-zh-hans language-pack-zh-hant && \
sudo dpkg-reconfigure locales && \
sudo update-locale LANG=zh_CN.UTF-8
pip install pylibreoffice
from pylibreoffice.py_office import PyOffice
class Example:
def __init__(self):
self.office = PyOffice("/usr/lib/libreoffice/program/")
def doc(self):
# Convert the doc file to pdf
print(self.office.save_as("./test.doc", "./test.pdf", "pdf"))
def xls(self):
# Convert the xls file to pdf
print(self.office.save_as("./test.xls", "./test_xls.pdf", "pdf"))
if __name__ == '__main__':
ex = Example()
ex.xls()
ex.doc()