从 Excel 中获取文本框值 Python

Obtain textbox value from Excel in Python

我有一个带有 the following pattern 的 Excel 文件 (data.xlsx),我需要从该 Excel 文件中的文本框中读取一个值。

我目前正在使用 pandas 库,我试图获取该值,但不幸的是找不到任何 API objective。

有谁知道这是怎么做到的?

更多信息:

我的问题与 Java 的 this 姐妹问题重复。

编辑:

我已经为想知道如何在 excel 文件中手动(即,没有来自 pip 的外部模块)。其实很简单。看我的评论。

我试过从文本框中获取值。

    xls = ExcelFile(request.FILES['yourFileName'])
    df = xls.parse(xls.sheet_names[0])
    for i in df.values:
        print(i[0])               #here you get the value from text box

谢谢

openpyxl(版本 2.4)目前无法做到这一点

感谢所有帮助,但我自己解决了这个问题。

我使用 zipfile 模块 让它工作。显然,Excel is actually a suite that works on compressed XML files (changing the *.xlsx to *.zip reveals the contents of the file) when saving and reading from *.xlsx,所以我可以轻松地搜索所需的文本,而 XML。

这是我制作的模块。通过调用 Sheet('path/to/sheet.xlsx').shapes.text,您现在可以轻松找到文本框内的文本:

import zipfile as z


class Sheet(str):
    @property
    def shapes(this):
        s = z.ZipFile(this)
        p='xl/drawings/drawing1.xml'  # shapes path, *.xlsx default
        p='drs/shapexml.xml'  # shapes path, *.xls default
        return XML(s.read(p))


class XML(object):
    def __init__(self, value):
        self.value = str(value)

    def __repr__(self):
        return repr(self.value)

    def __getitem__(self, i):
        return self.value[i]

    def tag_content(self, tag):
        return [XML(i) for i in self.value.split(tag)[1::2]]

    @property
    def text(self):
        t = self.tag_content('xdr:txBody')  # list of XML codes, each containing a seperate textboxes, messy (with extra xml that is)
        l = [i.tag_content('a:p>') for i in t]  # split into sublists by line breaks (inside the textbox), messy
        w = [[[h[1:-2] for h in i.tag_content('a:t')] if i else ['\n'] for i in j] for j in l]  # clean into sublists by cell-by-cell basis (and mind empty lines)
        l = [[''.join(i) for i in j] for j in w]  #  join lines overlapping multiple cells into one sublist
        return ['\n'.join(j) for j in l]  #  join sublists of lines into strings seperated by newline char

所以现在我的问题中提供的模式将输出为 ['comments extra'],而模式如:

  1. This is

    Text in a textbox on

    a sheet

  2. And this is another text box somewhere else

    Regardless of the overlapped cells

将输出为 ['This is\nText in a textbox on\na sheet','And this is another text box somewhere else\nRegardless of the overlapped cells']

不客气

您可以使用 Dispatch:

from win32com.client import Dispatch

xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Open(Filename = 'your file name/path')
ws = wb.Worksheets(sheet_index) 

其中 sheet_index 是与工作簿中感兴趣的 sheet 相对应的任何数字。 ws.Shapes 将拥有 sheet 上的所有形状对象。您可以使用整数 index, Shapes(index) 访问形状(文本框),然后检查对象的名称及其名称 属性 Shapes(index).Name .

ws.Shapes(index).Name    

一旦你确定了你想要的形状,你就可以像这样查看它的文字:

ws.Shapes(index).Characters().Text

请注意,您必须调用() Characters 方法。 要分配文本,只需分配它。或者您也可以使用标准替换方法替换它的一部分(如日期)。

ws.Shapes(index).Characters().Text = 'Beluga Whales'

ws.Shapes(index).TextFrame.Characters().Text = ws.Shapes(index).TextFrame.Characters().Text.replace('original text', 'new text')