小编gee*_*ers的帖子

在Python中打印word文档

我这里有一个简单的批处理文件,它将从命令行打印word文档.

"C:\Program Files\Microsoft Office\Office12\winword.exe" "p:\docs\daily checks.doc" /mFilePrintDefault /mFileExit
Run Code Online (Sandbox Code Playgroud)

我试图将其放入python脚本,我已设法通过使用打开文档

subprocess.Popen('"C:\\Program Files\Microsoft Office\Office12\winword.exe"' '"P:\\docs\\daily checks.doc "')
Run Code Online (Sandbox Code Playgroud)

我似乎无法获得/mFilePrintDefault /mFileExit命令中的部分,我尝试使用+'"/mFilePrintDefault /mFileExit"'plus而不使用+,但随后文档将无法打开.

你能帮我看看如何打印这个word文档,还是有更好的方法

提前致谢

python

3
推荐指数
1
解决办法
3427
查看次数

麻烦输出到文件

我这里有这个简单的代码

import os

path = (raw_input("Enter dir: "))   
f = open('script_list.log', 'w')
for dirpath, dirname, filenames in os.walk(path):
  for filename in [f for f in filenames]:
    f.write(str(filename) + "\n")
    print os.path.join(dirpath, filename)
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到以下内容

Enter dir: scripts
Traceback (most recent call last):
  File "C:\Documents and Settings\CRichards\My Documents\My Dropbox\this_code.py", line 8, in <module>
    f.write(str(filename) + "\n")
AttributeError: 'str' object has no attribute 'write'
Run Code Online (Sandbox Code Playgroud)

我知道它必须是简单的东西,我只是看不到它.

python

1
推荐指数
1
解决办法
59
查看次数

遇到参数问题

我试图编写这个小批量重命名文件扩展名的脚本.我传递三个参数,文件所在的目录,当前扩展名和新扩展名.

我得到的错误是

python batch_file_rename_2.py c:\craig .txt .html
Traceback (most recent call last):
  File "batch_file_rename_2.py", line 13, in <module>
  os.rename(filename, newfile) 
WindowsError: [Error 2] The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)

代码是

import os
import sys

work_dir=sys.argv[1]
old_ext=sys.argv[2]
new_ext=sys.argv[3]

files = os.listdir(work_dir)
for filename in files:
    file_ext = os.path.splitext(filename)[1]
    if old_ext == file_ext:
        newfile = filename.replace(old_ext, new_ext)
        os.rename(filename, newfile)
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
111
查看次数

总计似乎是错的

我有以下代码,简单我知道(请随时建议改进)

看来用shell脚本功能,总计似乎是错误的,我算22但是报告42,是否有代码问题.

import os

myPath = os.getenv("scripts")
pyCounter = 0 
sqlCounter = 0
shCounter = 0

def python_scripts():
  global pyCounter
  for root, dirs, files in os.walk(myPath):     
    for file in files:             
      if file.endswith('.py'):             
       pyCounter += 1 

def sql_scripts():
  global sqlCounter
  for root, dirs, files in os.walk(myPath):     
    for file in files:             
      if file.endswith('.sql'):             
       sqlCounter += 1 

def sh_scripts():
  global shCounter
  shell_ext = ['ksh','sh','bash']
  for shell in shell_ext:
    for root, dirs, files in os.walk(myPath):     
      for file in files:             
        if file.endswith(shell):
           shCounter += 1 …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
139
查看次数

Perl正则表达式示例不起作用

我只是想测试一下我在杂志上看过的东西(Linux Shell手册).我从未尝试过这样的事情,但我知道这可能有用

这个例子是

perl -n -e '/^The \s+(.*)$/ print "$1\n"' heroes.txt
Run Code Online (Sandbox Code Playgroud)

在heroes.txt中它有

Catwoman
Batman
The Tick
Spider-Man
Black Cat
Batgirl
Danger Girl
Wonder Woman
Luke Cage
Ant-Man
Spider-Woman
Run Code Online (Sandbox Code Playgroud)

这应该显示Tick,但是我得到了

perl -n -e '/^The \s+(.*)$/ print "$1\n"' heroes.txt
syntax error at -e line 1, near "/^The \s+(.*)$/ print"
Execution of -e aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

perl

-1
推荐指数
1
解决办法
171
查看次数

标签 统计

python ×4

perl ×1