自动化任务以在Python中将SPSS文件(.spv)转换为PDF

时间:2019-04-01 13:53:27

标签: python pdf spss rpa

我正在寻找一种将包含多个SPSS输出文件(.spv)的文件夹快速转换为PDF的方法。理想情况下,我想使用Python进行此操作,但是如果有一个更简单的解决方案,我将不胜枚举。

我真的只需要.spv文件即可在python中打开,然后单击文件->导出-> PDF。

我以前从未尝试过用任何一种语言来自动化任务,所以我对如何开始该工作一无所知。

1 个答案:

答案 0 :(得分:0)

Just for anyone with a similar question: I wasn't able to build an elegant converter or anything, but I did find an easy solution to just open up each file and click File -> Export -> PDF for me. I did this using PyAutoGUI

Here is my code for anyone interested.

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