在 Python 中自动执行将 SPSS 文件 (.spv) 转换为 PDF 的任务
Automate task to convert SPSS file (.spv) to PDF in Python
我正在寻找一种快速将包含多个 SPSS 输出文件 (.spv) 的文件夹转换为 PDF 的方法。理想情况下,我想用 Python 来做到这一点,但如果有更简单的解决方案,我会洗耳恭听。
我真的只需要在 python 中打开 .spv 文件,然后单击文件 -> 导出 -> PDF。
我以前从未尝试过使用任何语言自动执行任务,所以我什至不知道如何开始。
仅供有类似问题的任何人使用:我无法构建优雅的转换器或其他任何东西,但我确实找到了一个简单的解决方案,只需打开每个文件并为我单击文件 -> 导出 -> PDF .我使用 PyAutoGUI
做到了这一点
这是我的代码,供任何感兴趣的人使用。
from glob import glob
import pyautogui
import subprocess
import os
import time
import signal
all_files = glob("C://Users//directory//*//*")
screen = pyautogui.size()
#Confirm that your screen is 1920 X 1080
#If not, you'll need to change some of these dimensions
if screen == (1920,1080):
for file in all_files:
if file.endswith('.spv'):
#Have SPSS already open for this to work
p = subprocess.Popen(file, shell=True) #open file
#left to right 0 to 1920
#up to down 0 to 1080
pyautogui.moveTo(22, 50, duration=2) #File
pyautogui.click()
pyautogui.moveTo(22, 230) #Export
pyautogui.click()
pyautogui.moveTo(650, 250) #Choose 'All Visible' option
pyautogui.doubleClick()
pyautogui.doubleClick(800, 820) #Ok
try:
time.sleep(5) #Let SPSS Export, this takes time
#Rename the 'Output.pdf' file
#The "output_directory" is just where SPSS is saving your files
current_directory = os.path.dirname(os.path.realpath(file))
os.chdir("C://Users//output_directory")
for filename in os.listdir("C://Users//output_directory"):
if filename.startswith("OUTPUT.pdf"):
base = os.path.basename(os.path.normpath(file))
renamed, file_extension = os.path.splitext(base)
renamed = renamed + '.pdf'
os.rename(filename, renamed)
#Close SPSS file
pyautogui.click(2875, 5) #File
我正在寻找一种快速将包含多个 SPSS 输出文件 (.spv) 的文件夹转换为 PDF 的方法。理想情况下,我想用 Python 来做到这一点,但如果有更简单的解决方案,我会洗耳恭听。
我真的只需要在 python 中打开 .spv 文件,然后单击文件 -> 导出 -> PDF。
我以前从未尝试过使用任何语言自动执行任务,所以我什至不知道如何开始。
仅供有类似问题的任何人使用:我无法构建优雅的转换器或其他任何东西,但我确实找到了一个简单的解决方案,只需打开每个文件并为我单击文件 -> 导出 -> PDF .我使用 PyAutoGUI
做到了这一点这是我的代码,供任何感兴趣的人使用。
from glob import glob
import pyautogui
import subprocess
import os
import time
import signal
all_files = glob("C://Users//directory//*//*")
screen = pyautogui.size()
#Confirm that your screen is 1920 X 1080
#If not, you'll need to change some of these dimensions
if screen == (1920,1080):
for file in all_files:
if file.endswith('.spv'):
#Have SPSS already open for this to work
p = subprocess.Popen(file, shell=True) #open file
#left to right 0 to 1920
#up to down 0 to 1080
pyautogui.moveTo(22, 50, duration=2) #File
pyautogui.click()
pyautogui.moveTo(22, 230) #Export
pyautogui.click()
pyautogui.moveTo(650, 250) #Choose 'All Visible' option
pyautogui.doubleClick()
pyautogui.doubleClick(800, 820) #Ok
try:
time.sleep(5) #Let SPSS Export, this takes time
#Rename the 'Output.pdf' file
#The "output_directory" is just where SPSS is saving your files
current_directory = os.path.dirname(os.path.realpath(file))
os.chdir("C://Users//output_directory")
for filename in os.listdir("C://Users//output_directory"):
if filename.startswith("OUTPUT.pdf"):
base = os.path.basename(os.path.normpath(file))
renamed, file_extension = os.path.splitext(base)
renamed = renamed + '.pdf'
os.rename(filename, renamed)
#Close SPSS file
pyautogui.click(2875, 5) #File