41

*.xls (excel 2003) ファイルがいくつかあり、これらのファイルを xlsx (excel 2007) に変換したいと考えています。

私は uno python パッケージを使用します。ドキュメントを保存するときに、フィルタ名を設定できます: MS Excel 97 しかし、「MS Excel 2007」のようなフィルタ名はありません。

xls を xlsx に変換するフィルター名を設定するにはどうすればよいですか?

4

17 に答える 17

35

マシンに win32com をインストールする必要があります。これが私のコードです:

import win32com.client as win32
fname = "full+path+to+xls_file"
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(fname)

wb.SaveAs(fname+"x", FileFormat = 51)    #FileFormat = 51 is for .xlsx extension
wb.Close()                               #FileFormat = 56 is for .xls extension
excel.Application.Quit()
于 2014-06-29T13:48:07.830 に答える
25

フォント、チャート、画像を考慮しない私の解決策は次のとおりです。

$ pip install pyexcel pyexcel-xls pyexcel-xlsx

次に、これを行います::

import pyexcel as p

p.save_book_as(file_name='your-file-in.xls',
               dest_file_name='your-new-file-out.xlsx')

プログラムが必要ない場合は、1 つの追加パッケージ pyexcel-cli:: をインストールできます。

$ pip install pyexcel-cli
$ pyexcel transcode your-file-in.xls your-new-file-out.xlsx

上記のトランスコード手順では、xlrd と openpyxl を使用しています。

于 2017-04-10T14:22:00.400 に答える
23

私は前にこれをしなければなりませんでした。主なアイデアは、xlrdモジュールを使用してxlsファイルを開いて解析し、openpyxlモジュールを使用してコンテンツをxlsxファイルに書き込むことです。

これが私のコードです。注意!複雑なxlsファイルを処理することはできません。使用する場合は、独自の解析ロジックを追加する必要があります。

import xlrd
from openpyxl.workbook import Workbook
from openpyxl.reader.excel import load_workbook, InvalidFileException

def open_xls_as_xlsx(filename):
    # first open using xlrd
    book = xlrd.open_workbook(filename)
    index = 0
    nrows, ncols = 0, 0
    while nrows * ncols == 0:
        sheet = book.sheet_by_index(index)
        nrows = sheet.nrows
        ncols = sheet.ncols
        index += 1

    # prepare a xlsx sheet
    book1 = Workbook()
    sheet1 = book1.get_active_sheet()

    for row in xrange(0, nrows):
        for col in xrange(0, ncols):
            sheet1.cell(row=row, column=col).value = sheet.cell_value(row, col)

    return book1
于 2012-03-29T05:11:08.293 に答える
11

ここで100%正しい答えは見つかりませんでした。だから私は私のコードをここに投稿します:

import xlrd
from openpyxl.workbook import Workbook

def cvt_xls_to_xlsx(src_file_path, dst_file_path):
    book_xls = xlrd.open_workbook(src_file_path)
    book_xlsx = Workbook()

sheet_names = book_xls.sheet_names()
for sheet_index, sheet_name in enumerate(sheet_names):
    sheet_xls = book_xls.sheet_by_name(sheet_name)
    if sheet_index == 0:
        sheet_xlsx = book_xlsx.active
        sheet_xlsx.title = sheet_name
    else:
        sheet_xlsx = book_xlsx.create_sheet(title=sheet_name)

    for row in range(0, sheet_xls.nrows):
        for col in range(0, sheet_xls.ncols):
            sheet_xlsx.cell(row = row+1 , column = col+1).value = sheet_xls.cell_value(row, col)

book_xlsx.save(dst_file_path)
于 2016-09-13T03:33:09.377 に答える
7

Ray の回答は非常に役に立ちましたが、すべてのシートを xls から xlsx に変換する簡単な方法を探している人のために、このGistを作成しました。

import xlrd
from openpyxl.workbook import Workbook as openpyxlWorkbook

# content is a string containing the file. For example the result of an http.request(url).
# You can also use a filepath by calling "xlrd.open_workbook(filepath)".

xlsBook = xlrd.open_workbook(file_contents=content)
workbook = openpyxlWorkbook()

for i in xrange(0, xlsBook.nsheets):
    xlsSheet = xlsBook.sheet_by_index(i)
    sheet = workbook.active if i == 0 else workbook.create_sheet()
    sheet.title = xlsSheet.name

    for row in xrange(0, xlsSheet.nrows):
        for col in xrange(0, xlsSheet.ncols):
            sheet.cell(row=row, column=col).value = xlsSheet.cell_value(row, col)

# The new xlsx file is in "workbook", without iterators (iter_rows).
# For iteration, use "for row in worksheet.rows:".
# For range iteration, use "for row in worksheet.range("{}:{}".format(startCell, endCell)):".

xlrd libはここに、openpyxlはここにあります (たとえば、Google App Engine のプロジェクトで xlrd をダウンロードする必要があります)。

于 2015-06-10T09:35:21.883 に答える
0

最初に@Jhonのソリューションを試した後、ソリューションとしてpyexcelになりました

pyexcel.save_as(file_name=oldfilename, dest_file_name=newfilename)

PyInstaller によってプロジェクトを単一の exe ファイルにパッケージ化しようとするまでは正常に動作し、すべての非表示のインポート オプションを試しましたが、エラーはまだ残っています。

  File "utils.py", line 27, in __enter__
    pyexcel.save_as(file_name=self.filename, dest_file_name=newfilename)
  File "site-packages\pyexcel\core.py", line 77, in save_as
  File "site-packages\pyexcel\internal\core.py", line 22, in get_sheet_stream
  File "site-packages\pyexcel\plugins\sources\file_input.py", line 39, in get_da
ta
  File "site-packages\pyexcel\plugins\parsers\excel.py", line 19, in parse_file
  File "site-packages\pyexcel\plugins\parsers\excel.py", line 40, in _parse_any
  File "site-packages\pyexcel_io\io.py", line 73, in get_data
  File "site-packages\pyexcel_io\io.py", line 91, in _get_data
  File "site-packages\pyexcel_io\io.py", line 188, in load_data
  File "site-packages\pyexcel_io\plugins.py", line 90, in get_a_plugin
  File "site-packages\lml\plugin.py", line 290, in load_me_now
  File "site-packages\pyexcel_io\plugins.py", line 107, in raise_exception
pyexcel_io.exceptions.SupportingPluginAvailableButNotInstalled: Please install p
yexcel-xls
[3192] Failed to execute script

次に、パンダにジャンプしました:

pd.read_excel(oldfilename).to_excel(newfilename, sheet_name=self.sheetname,index=False)

2020 年 2 月 21 日更新

openpyxlは次の機能を提供します:

行を xlxs ファイルに挿入する機能を有効にします。これは、ユーザーが xls ファイルからデータを読み取って xlsx ファイルに挿入できることを意味します。

  • append(['これは A1', 'これは B1', 'これは C1'])
  • または append({'A' : 'This is A1', 'C' : 'This is C1'})
  • または append({1 : 'これは A1 です', 3 : 'これは C1 です'})

現在のシートの下部に値のグループを追加します。

  • リストの場合: すべての値が最初の列から順番に追加されます
  • 辞書の場合: キー (数字または文字) で示される列に値が割り当てられます
于 2019-06-10T11:45:36.193 に答える
0

The Answer from Ray was clipping the first row and last column of the data. Here is my modified solution (for python3):

def open_xls_as_xlsx(filename):
# first open using xlrd
book = xlrd.open_workbook(filename)
index = 0
nrows, ncols = 0, 0
while nrows * ncols == 0:
    sheet = book.sheet_by_index(index)
    nrows = sheet.nrows+1   #bm added +1
    ncols = sheet.ncols+1   #bm added +1
    index += 1

# prepare a xlsx sheet
book1 = Workbook()
sheet1 = book1.get_active_sheet()

for row in range(1, nrows):
    for col in range(1, ncols):
        sheet1.cell(row=row, column=col).value = sheet.cell_value(row-1, col-1) #bm added -1's

return book1
于 2017-04-11T03:37:41.563 に答える