在Python中自动执行将SPSS文件(.spv)转换为PDF的任务

2024-09-25 00:30:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种方法来快速转换包含多个SPSS输出文件(.spv)的文件夹到pdf。理想情况下,我希望使用Python来实现这一点,但如果有更简单的解决方案,我会全神贯注的。在

我只需要在python中打开.spv文件,然后单击“文件”—>;“导出”—>;“PDF”。在

我以前从来没有尝试过用任何语言实现任务的自动化,所以我不知道如何开始。在


Tags: 文件方法gt文件夹语言pdf情况解决方案
1条回答
网友
1楼 · 发布于 2024-09-25 00:30:11

对于任何有类似问题的人:我不能构建一个优雅的转换器或任何东西,但我确实找到了一个简单的解决方案,只需打开每个文件,然后单击“文件”—“导出”—>;“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

相关问题 更多 >