[英]ImportError: cannot import name 'COMError' from '_ctypes' (/usr/lib/python3.7/lib-dynload/_ctypes.cpython-37m-x86_64-linux-gnu.so)
import os
import glob
import comtypes.client
from PyPDF2 import PdfFileMerger
def docxs_to_pdf():
"""Converts all word files in pdfs and append them to pdfslist"""
word = comtypes.client.CreateObject('Word.Application')
pdfslist = PdfFileMerger()
x = 0
for f in glob.glob("*.docx"):
input_file = os.path.abspath(f)
output_file = os.path.abspath("demo" + str(x) + ".pdf")
# loads each word document
doc = word.Documents.Open(input_file)
doc.SaveAs(output_file, FileFormat=16+1)
doc.Close() # Closes the document, not the application
pdfslist.append(open(output_file, 'rb'))
x += 1
word.Quit()
return pdfslist
def joinpdf(pdfs):
"""Unite all pdfs"""
with open("result.pdf", "wb") as result_pdf:
pdfs.write(result_pdf)
def main():
"""docxs to pdfs: Open Word, create pdfs, close word, unite pdfs"""
pdfs = docxs_to_pdf()
joinpdf(pdfs)
main()
I am using jupyter notebook and it throw an error what should I do: this is error message我正在使用 jupyter notebook,它抛出错误我该怎么办:这是错误消息
I am going to convert many.doc file to one pdf. Help me I am beginner in this field.我要将 many.doc 文件转换为一个 pdf。帮助我,我是这个领域的初学者。
Make sure you have all the dependencies installed in your environment.确保您的环境中安装了所有依赖项。 You can use pip to install comtypes.client, simply pass this in your terminal:
您可以使用 pip 安装 comtypes.client,只需将其传递到您的终端即可:
pip install comtypes
You can download _ctypes from sourceforge: https://sourceforge.net/projects/ctypes/files/ctypes/1.0.2/ctypes-1.0.2.tar.gz/download?use_mirror=deac-fra您可以从 sourceforge 下载 _ctypes: https://sourceforge.net/projects/ctypes/files/ctypes/1.0.2/ctypes-1.0.2.tar.gz/download?use_mirror=deac-fra
Using docx2pdf does seem easier for your task though.不过,使用 docx2pdf 似乎更容易完成您的任务。 After you converted the files you can use PyPDF2 to append them.
转换文件后,您可以使用 PyPDF2 将其转换为 append。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.