“WindowsPath”类型的对象无法转换为COM VARIANT

问题描述 投票:-1回答:1

我有一个xlsx文件,它是收据的模板。它包含图像和单元格。我曾经手动进入文件,更新信息,然后在发送给我的客户之前导出为pdf。如果可能的话,我希望能够通过python将xlsx转换为pdf。

我的问题是没有人显示只选择xlsx文件并将其更改为pdf的教程。或者没有像样的视频教程。

我已经尝试将openpyxl保存为.pdf的扩展名,但我知道这是一个很长的镜头。我试图按照堆栈溢出的例子,但它没有那么好用。

我一直在:

File "<COMObject <unknown>>", line 5, in ExportAsFixedFormat 
Objects of type 'WindowsPath' can not be converted to a COM VARIANT

而且我很困惑。

#this file will open a wb and save it as another file name
#this first part opens a file from a location and makes a copy to another location

from pathlib import Path
from win32com import client

#sets filename and file
file_name = 'After Summer Bookings.xlsx'
dir_path = Path('C:/Users/BOTTL/Desktop/Business')
new_file_name = 'hello.pdf'
new_save_place = Path('C:/Users/BOTTL/Desktop/Business Python')

xlApp = client.Dispatch("Excel.Application")
books = xlApp.Workbooks.Open(dir_path / file_name)
ws = books.Worksheets[0]
ws.Visible = 1
ws.ExportAsFixedFormat(0, new_save_place / new_file_name)

我想打开我称之为After Summer Bookings.xlsx的xlsx文件并将其保存为名为hello.pdf的pdf文件

python pdf xlsx win32com
1个回答
1
投票

自己解决了:)

from pathlib import Path
from win32com import client

#sets filename and file
file_name = 'After Summer Bookings.xlsx'
dir_path = Path('C:/Users/BOTTL/Desktop/Business')
new_file_name = 'hello.pdf'
new_save_place = ('C:/Users/BOTTL/Desktop/Business Python/')
path_and_place = new_save_place + new_file_name

xlApp = client.Dispatch("Excel.Application")
books = xlApp.Workbooks.Open(dir_path / file_name)
ws = books.Worksheets[0]
ws.Visible = 1
ws.ExportAsFixedFormat(0,path_and_place)

当连接位置和文件名时,它不喜欢我已经使它成为一条路径,所以现在我删除了路径,它就像一个梦想:)

© www.soinside.com 2019 - 2024. All rights reserved.