我一直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()
Run Code Online (Sandbox Code Playgroud)
但是,我已经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)
Run Code Online (Sandbox Code Playgroud)
在Python中是否有很好的替代方法可以将转换xlsx为PDF?
我已经使用PDFWriter测试了xtopdf,但是使用此解决方案,您需要读取和迭代范围并逐行写入行。我想知道是否有类似于win32com.client的更直接的解决方案。
谢谢!
我在运行模块 gasprop 时遇到此错误。我不明白错误的含义以及如何修复它:
import gasprop
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "gasprop.py", line 13, in <module>
sheet = wb.Sheets("Input1")
File "C:\Python27\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x6\Sheets.py", line 113, in __call__
ret = self._oleobj_.InvokeTypes(0, LCID, 2, (9, 0), ((12, 1),),Index
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)
Run Code Online (Sandbox Code Playgroud)
这是我的模块 gasprop:
import win32com.client, os
xl = win32com.client.gencache.EnsureDispatch("Excel.Application")
thisdir = os.getcwd()
wb = xl.Workbooks.Open(thisdir+"/Input1.xlsx")
sheet = wb.Sheets("Input1")
......
def megaverify(self):
listtr,listp=[],[]
for i in range(16):
tr=float(sheet.Range("D"+str(5+i)).Value)
p=float(sheet.Range("C"+str(5+i)).Value)
listtr.append(tr);listp.append(p)
return …Run Code Online (Sandbox Code Playgroud)