首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动任务,以转换SPSS文件(.spv)到PDF在Python

自动任务,以转换SPSS文件(.spv)到PDF在Python
EN

Stack Overflow用户
提问于 2019-04-01 21:53:27
回答 1查看 557关注 0票数 1

我正在寻找一种方法来快速转换包含多个SPSS输出文件(.spv)到PDF的文件夹。理想情况下,我希望使用Python来完成这项工作,但如果有更简单的解决方案,我会洗耳恭听。

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

我从来没有尝试过在任何语言中自动执行任务,所以我甚至不知道如何开始这项工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-08 21:00:00

对于任何有类似问题的人:我无法建立一个优雅的转换器或任何东西,但我找到了一个简单的解决方案,只需打开每个文件,然后单击文件->导出-> PDF为我。我使用PyAutoGUI做到了这一点

这是我的代码,供感兴趣的人参考。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55456831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档