在Python中从csv编写xlsx文件

时间:2018-07-13 20:58:12

标签: python xlsx

这是我的代码,该代码从doc Excel文件加载电子表格并创建csvTranslations.csv,transitions.py和translations.xlsx。问题是它正在创建一个空白的translattions.xlsx文件,但我不知道我在做什么错。

import os
import random
import re
import csv
import requests
import io
import sys
import copy
import xlrd
import codecs
import time
import xlrd
from collections import OrderedDict
import simplejson as json
import xlsxwriter
from pandas.io.excel import ExcelWriter
import pandas
import openpyxl
from openpyxl import Workbook
from openpyxl.utils  import get_column_letter
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
  

将被调用的功能

def gen_translations():
    try:
        import xlrd
    except:
        print "Missing xlrd module!"
        return
    #return
    csv_file = 'translations.xlsx'
    if not os.path.exists(csv_file):
        print "Unable to update translations due to missing csv file!"
        return

    print "updating translations..."
    o = open("translations.py", "wb")
  

链接到Google文档

url = "https://docs.google.com/spreadsheets/d/1g3nk_yzQxQFSxPN2lCvWwiHH8MgAqhpu5S763FRD_5I/gviz/tq?tqx=out:csv&sheet=translations"
    r = requests.get(url, verify=False)

    ow = io.open("csvTranslations.csv", "wb")

    ow.write(r.content)

    ow.close()



    try:
        print >> o, header
    except:
        print >> o, "#CANNOT PRINT HEADER:%s" % repr(header)

    with open("csvTranslations.csv", "rb") as csvfile:
        docreader = csv.reader(csvfile)
        headers = next(docreader)
        for row in docreader:
            decoded_row = [entry.decode("utf8") for entry in row]
            decoded_row[6] = translate(decoded_row[2], to_X)

            try:
                id = int(decoded_row[0])
            except:
                break
            keyword = decoded_row[1]
            languages = decoded_row[2:]
            '''
            try:
                print u" ; ".join(languages)
            except:
                print "#CANNOT PRINT:%s"%repr(languages)
            '''
            try:
                print >> o, "    %s = Word(%d, %s)" % (keyword, id, languages)
            except:
                print >> "ERROR: CANNOT PRINT %s = Word(%d,%s)" % (repr(keyword), id, repr(languages))
        try:
            print >> o, footer
        except:
            print >> o, "#ERROR CANNOT PRINT FOOTER:%s" % repr(footer)
        o.close()
        print "done!"
  

问题似乎出在这里,因为同时编写了csvTranslations和translations.py,但不是translations.xlsx

        csv.register_dialect('colons', delimiter=',')

        reader = csv.reader(csvfile, dialect='colons')

        wb = Workbook()
        dest_filename = r"translations.xlsx"

        ws = wb.worksheets[0]

        for row_index, row in enumerate(reader):
            for column_index, cell in enumerate(row):
                column_letter = get_column_letter((column_index + 1))
                ws['%s%s'%(column_letter, (row_index + 1))].value = cell

        wb.save(filename=dest_filename)

1 个答案:

答案 0 :(得分:0)

您似乎要尝试两次阅读“ csvTranslations.csv”,但是您只打开了一次文件。因此,一旦脚本完成读取操作以写入translations.py,则csvTranslations.csv的文件指针现在位于文件末尾,并且进一步尝试从csvfile读取的操作均返回no数据(相反,它们指示“您已经在文件末尾!”,导致您在for row_index, row in enumerate(reader):处的循环退出。)解决此问题的最简单方法是第二次打开csvTranslations.csv ,位于reader = csv.reader(csvfile, dialect='colons')行之前;这为您提供了一个指向文件的新指针,该指针从头开始。