'WindowsPath' 类型的对象无法转换为 COM VARIANT
Objects of type 'WindowsPath' can not be converted to a COM VARIANT
我有一个 xlsx 文件,它是收据的模板。它包含图像和单元格。我曾经手动进入文件,更新信息,然后在发送给我的客户之前导出为 pdf。如果可能的话,我希望能够通过 python 将 xlsx 转换为 pdf。
我的问题是没有人显示仅选择 xlsx 文件并将其更改为 pdf 的教程。或者没有像样的视频教程。
我已经尝试让 openpyxl 将其保存为 .pdf 的扩展名,但我知道这是一个很长的路要走。我尝试按照堆栈溢出的示例进行操作,但效果不佳。
I keep getting :
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 文件
自己解决了:)
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)
当连接位置和文件名时,它不喜欢我将其设为路径,所以现在我删除了路径,它就像一个梦:)
我有一个 xlsx 文件,它是收据的模板。它包含图像和单元格。我曾经手动进入文件,更新信息,然后在发送给我的客户之前导出为 pdf。如果可能的话,我希望能够通过 python 将 xlsx 转换为 pdf。
我的问题是没有人显示仅选择 xlsx 文件并将其更改为 pdf 的教程。或者没有像样的视频教程。
我已经尝试让 openpyxl 将其保存为 .pdf 的扩展名,但我知道这是一个很长的路要走。我尝试按照堆栈溢出的示例进行操作,但效果不佳。
I keep getting :
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
自己解决了:)
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)
当连接位置和文件名时,它不喜欢我将其设为路径,所以现在我删除了路径,它就像一个梦:)