简体   繁体   English

使用python填充excel文件会弄乱我的日期

[英]Filling an excel file with python is messing up my dates

Here is the process, I have an xlsm file called template, inside a worksheet called data. 这是一个过程,我在一个名为data的工作表中有一个名为template的xlsm文件。 Then I have 5 Csv files, I must put the content of all csv file inside the data worksheet. 然后我有5个Csv文件,我必须将所有csv文件的内容放在数据工作表中。

No problems to do that, except if a date is "07/09/12" it will become "09/07/12" in excel. 没问题,除非日期是“07/09/12”,它将成为excel中的“09/07/12”。

So I have a list called data, here is a print of one of it's line 所以我有一个名为data的列表,这里是其中一行的打印

['07/09/12', 'LARO', 'MEDITERRAN', '245', 'UZES', '11', '0', '0', '0', '0', '11', '0']

I'm putting the in the excel file with this piece of code : 我正在使用这段代码放入excel文件:

First I open the file (xl is because I use "with self as xl") 首先我打开文件(xl是因为我使用“with self as xl”)

def open(self):
    self.xl.Visible = 1
    self.xl.ScreenUpdating = False
    self.worksheet = self.xl.Workbooks.Open(self.getOutputFile())

Then I erase the data worksheet 然后我擦除数据工作表

def cleanData(self):
    sheet = self.xl.Sheets(self.sheetName)
    def findNumberOfLines(start_line):
        nb = 0
        while sheet.Cells(start_line + nb, self.startColumn).Value is not None:
            nb += 1
        return nb

Then I fill the data 然后我填写数据

def fillData(self):
    sheet = self.xl.Sheets(self.sheetName)
    noRow = self.startLine
    for row in self.data:
        noCol = self.startColumn
        for i, value in enumerate(row):
            sheet.Cells(noRow, noCol).Value = value
            noCol+=1
        noRow+=1

And I save 我救了

def save(self):
    self.worksheet.Save()

And everythong is good except the day and month are inverted inside excel (it's not displaying US format, it's displaying EUR format with month and day inverted (3 october becomes 9 march, tested using "long date" format) 并且每一件事都很好,除了日期和月份在excel内部反转(它没有显示美国格式,它显示欧元格式,月和日倒置(10月3日变为9月3日,使用“长日期”格式测试)

Weird thing : the excel cell containing dates is set at "standard" but once it has been filed by my python script it's set at "date" 奇怪的是:包含日期的excel单元格被设置为“标准”但是一旦它被我的python脚本提交,它就被设置为“date”

WORKAROUND when filling the sheet I'm intercepting every date and converting them to datetime, excel seems to like that 填写表单时的替代方法我正在拦截每个日期并将它们转换为日期时间,excel似乎就是这样

def fillData(self):
    sheet = self.xl.Sheets(self.sheetName)
    noRow = self.startLine
    pattern = re.compile('^\d+/\d+/\d+$')
    for row in self.data:
        noCol = self.startColumn
        for i, value in enumerate(row):

            if (pattern.match(str(value))):
                date = value.rsplit('/')
                sheet.Cells(noRow, noCol).Value = datetime(int(date[2]), int(date[1]), int(date[0]))
            else: sheet.Cells(noRow, noCol).Value = value
            noCol+=1
        noRow+=1

Unless you tell Excel, it tries to be "smart" with dates which means it will guess what the format is and how to display the converted date. 除非你告诉Excel,否则它会尝试使用日期“智能”,这意味着它会猜测格式是什么以及如何显示转换日期。

In almost all cases, you don't want that. 在几乎所有情况下,你都不希望如此。 Such files will display differently on computers with different date formats or when users have changed the OS default (even some Americans prefer DD/MM/YY over the standard MM/DD/YY). 这些文件在具有不同日期格式的计算机上或用户更改操作系统默认值时会以不同方式显示(甚至一些美国人更喜欢DD / MM / YY而不是标准MM / DD / YY)。

So the correct solution is to datetime instead of string values. 所以正确的解决方案是使用datetime而不是字符串值。 This works, btw. 这很有效,顺便说一下。 because the COM framework inside of win32com will convert the Python datetime object to the corresponding Excel date object plus set the formatting correctly. 因为win32com的COM框架会将Python datetime对象转换为相应的Excel日期对象,并正确设置格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM