【问题标题】:listing filenames in a directory with .docx extension using python使用 python 列出具有 .docx 扩展名的目录中的文件名
【发布时间】:2018-03-03 18:20:24
【问题描述】:

'''这个脚本是将文本从文档(docx)复制到简单的文本文件

'''

import sys
import ntpath
import os
from docx import Document

docpath = os.path.abspath(r'C:\Users\Khairul Basar\Documents\CWD Projects\00_WORKING\WL_SLOT1_submission_date_30-03-2018\1-100')
txtpath = os.path.abspath(r'C:\Users\Khairul Basar\Documents\CWD Projects\00_WORKING\WL_SLOT1_submission_date_30-03-2018\Textfiles')

for filename in os.listdir(docpath):
    try:
        document = Document(os.path.join(docpath, filename))
        # print(document.paragraphs)
        print(filename)
        savetxt = os.path.join(txtpath, ntpath.basename(filename).split('.')[0] + ".txt")
        print('Reading ' + filename)
        # print(savetxt)
        fullText = []
        for para in document.paragraphs:
            # print(para.text)
            fullText.append(para.text)
        with open(savetxt, 'wt') as newfile:
            for item in fullText:
                newfile.write("%s\n" % item)
        # with open(savetxt, 'a') as f:
        # f.write(para.text)
        # print(" ".join([line.rstrip('\n') for line in f]))
        # newfile.write(fullText)
        # newfile.save()
        # newfile.save()
        #
        # newfile.write('\n\n'.join(fullText))
        # newfile.close()

    except:
        # print(filename)
        # document = Document(os.path.join(docpath, filename))
        # print(document.paragraphs)
        print('Please fix an error')
        exit()

    # print("Please supply an input and output file. For example:\n"
 # #  "  example-extracttext.py 'My Office 2007 document.docx' 'outp"
 #   "utfile.txt'")

    # Fetch all the text out of the document we just created

    # Make explicit unicode version

    # Print out text of document with two newlines under each paragraph

print(savetxt)

以上 python 3 脚本是读取 Docx 文件并创建 txt 文件。在一个目录中,我有 100 个 docx 文件,但它只创建 19 个 txt 文件,然后退出程序。我不知道为什么?

Docx 文件是 OCR 软件的输出文件,都是英文文本(没有图像、表格或图形或特殊的东西)。

今天我在删除 Try/Except 指令后再次运行程序,结果相同:

1.docx
阅读1.docx
10.docx
阅读10.docx
100.docx
阅读100.docx
11.docx
阅读11.docx
12.docx
阅读12.docx
13.docx
阅读13.docx
14.docx
阅读14.docx
15.docx
阅读15.docx
16.docx
阅读16.docx
17.docx
阅读17.docx
18.docx
阅读18.docx
回溯(最近一次通话最后一次):
文件“C:\Users\Khairul Basar\Documents\CWD Projects\docx2txtv2.py”,第 26 行,

newfile.write("%s\n" % item)
文件“C:\Python36\lib\encodings\cp1252.py”,第 19 行,编码
返回 codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError:“charmap”编解码器无法在位置编码字符“\u0113”
77:字符映射到

其他一些帖子 Here 通过 .encode("utf-8") 解决这个问题 但是如果我使用它,那么我会在每一行中得到 b'my text' - 这是我不需要的。

更新已修复

我已对以下行进行了更改: 使用 open(savetxt, 'w', encoding='utf-8') 作为新文件:

通过添加 encoding='utf-8'

我从这篇文章中得到了帮助。 post

感谢您以一种很好的方式整理了我的帖子。

【问题讨论】:

  • 对不起,上面的文字搞砸了。这是python代码文件:drive.google.com/open?id=19QISE7aSS4m7lczr5Dr6NcKF9KEqU18f
  • 在您之前的问题中,格式化代码没有问题,但以防万一您忘记了(并且还设法忽略了编辑器周围的各种帮助、格式和预览选项):How to format your post using Markdown and HTML。除此之外,没有足够的信息来提出答案。这些文件有什么特别之处,除了那些有效的 19 个文件吗?文件类型?姓名?尺寸?内容?
  • (续):需要的第一步:删除try/except,这样您就可以真正查看文件失败的原因...
  • 我按照 usr2564301 的建议进行了更改,但得到了相同的结果。
  • 是的,你当然知道。但这不建议作为“修复”!现在您知道问题出在哪里以及发生在哪里,而不是“它不起作用”。

标签: python-3.x file docx


【解决方案1】:

usr2564301 指出要从代码中删除 Try/except。通过这样做,我得到了确切的错误,为什么它不工作或过早退出程序。

问题是我的 Docx 有许多超出 8 位字符集的字符。要将非英文字符转换为英文 encoding='utf-8' 。

这解决了问题。

无论如何,所有功劳都归功于我不知道的 usr2564301。

【讨论】:

  • :) 只是试图引导您找到可能的解决方案。很高兴你能找到它!
猜你喜欢
  • 2012-02-24
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 2018-04-27
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
相关资源
最近更新 更多