【发布时间】:2019-02-18 23:03:16
【问题描述】:
我一直在我的开发服务器中使用win32com 模块来轻松地从xlsx 转换为pdf:
o = win32com.client.Dispatch("Excel.Application")
o.Visible = False
o.DisplayAlerts = False
wb = o.Workbooks.Open("test.xlsx")))
wb.WorkSheets("sheet1").Select()
wb.ActiveSheet.ExportAsFixedFormat(0, "test.pdf")
o.Quit()
但是,我已将我的 Django 应用程序部署在没有安装 Excel 应用程序的生产服务器中,它会引发以下错误:
File "C:\virtualenvs\structuraldb\lib\site-packages\win32com\client\__init__.p
y", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,c
lsctx)
File "C:\virtualenvs\structuraldb\lib\site-packages\win32com\client\dynamic.py
", line 114, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\virtualenvs\structuraldb\lib\site-packages\win32com\client\dynamic.py
", line 91, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.II
D_IDispatch)
com_error: (-2147221005, 'Invalid class string', None, None)
在 Python 中从 xlsx 转换为 PDF 有什么好的替代方法吗?
我已经使用 PDFWriter 测试了 xtopdf,但是使用此解决方案,您需要读取和迭代范围并逐行写入。不知有没有类似win32com.client的更直接的解决方案。
谢谢!
【问题讨论】:
-
不是,例外不同。顺便说一句,那个帖子还没有解决。
-
是的……你有一个可移植性问题,并且该线程列出了将 xlsx 转换为 pdf 的所有可行选项。顺便说一句,OP 回答了他们自己的问题。
-
是的,他使用了对我不起作用的相同解决方案。因为这不是同一个问题,所以我打开了一个新线程。
标签: python django pdf xlsx win32com